Fix binary tree node.

This commit is contained in:
Oleksii Trekhleb
2018-04-05 06:51:45 +03:00
parent 7f64f55397
commit b24763e249
2 changed files with 11 additions and 1 deletions

View File

@ -7,7 +7,7 @@ export default class BinaryTreeNode {
}
get height() {
if (!this.left && !this.left) {
if (!this.left && !this.right) {
return 0;
}

View File

@ -140,4 +140,14 @@ describe('BinaryTreeNode', () => {
expect(grandRight.height).toBe(0);
expect(grandGrandLeft.height).toBe(0);
});
it('should calculate node height for right nodes as well', () => {
const root = new BinaryTreeNode(1);
const right = new BinaryTreeNode(2);
root.setRight(right);
expect(root.height).toBe(1);
expect(right.height).toBe(0);
});
});