mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add possibility for tree nodes to have height.
This commit is contained in:
@@ -6,6 +6,17 @@ export default class BinaryTreeNode {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
get height() {
|
||||
if (!this.left && !this.left) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const leftHeight = this.left ? this.left.height : 0;
|
||||
const rightHeight = this.right ? this.right.height : 0;
|
||||
|
||||
return Math.max(leftHeight, rightHeight) + 1;
|
||||
}
|
||||
|
||||
setLeft(node) {
|
||||
this.left = node;
|
||||
this.left.parent = this;
|
||||
|
||||
Reference in New Issue
Block a user