Fix binary tree node.

This commit is contained in:
Oleksii Trekhleb 2018-04-05 08:29:13 +03:00
parent b24763e249
commit d19149de8e

View File

@ -6,15 +6,24 @@ export default class BinaryTreeNode {
this.value = value; this.value = value;
} }
get height() { get leftHeight() {
if (!this.left && !this.right) { if (!this.left) {
return 0; return 0;
} }
const leftHeight = this.left ? this.left.height : 0; return this.left.height + 1;
const rightHeight = this.right ? this.right.height : 0; }
return Math.max(leftHeight, rightHeight) + 1; get rightHeight() {
if (!this.right) {
return 0;
}
return this.right.height + 1;
}
get height() {
return Math.max(this.leftHeight, this.rightHeight);
} }
setLeft(node) { setLeft(node) {