Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b24763e

Browse files
committedApr 5, 2018
Fix binary tree node.
1 parent 7f64f55 commit b24763e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed
 

‎src/data-structures/tree/BinaryTreeNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class BinaryTreeNode {
77
}
88

99
get height() {
10-
if (!this.left && !this.left) {
10+
if (!this.left && !this.right) {
1111
return 0;
1212
}
1313

‎src/data-structures/tree/__test__/BinaryTreeNode.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,14 @@ describe('BinaryTreeNode', () => {
140140
expect(grandRight.height).toBe(0);
141141
expect(grandGrandLeft.height).toBe(0);
142142
});
143+
144+
it('should calculate node height for right nodes as well', () => {
145+
const root = new BinaryTreeNode(1);
146+
const right = new BinaryTreeNode(2);
147+
148+
root.setRight(right);
149+
150+
expect(root.height).toBe(1);
151+
expect(right.height).toBe(0);
152+
});
143153
});

0 commit comments

Comments
 (0)
Please sign in to comment.