mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-06 17:44:08 +08:00
Fix binary tree node.
This commit is contained in:
@ -7,7 +7,7 @@ export default class BinaryTreeNode {
|
||||
}
|
||||
|
||||
get height() {
|
||||
if (!this.left && !this.left) {
|
||||
if (!this.left && !this.right) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user