Skip to content

Commit c4a733a

Browse files
committed
update: height of BST
1 parent c9e3a6a commit c4a733a

File tree

1 file changed

+35
-0
lines changed
  • src/_DataStructures_/Trees/BinarySearchTree/height-of-bst

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const BST = require('../index');
3+
4+
function findHeightOfBST(root) {
5+
let leftHeight = 0;
6+
let rightHeight = 0;
7+
8+
if (root === null) return 0;
9+
leftHeight = findHeightOfBST(root.leftChild);
10+
rightHeight = findHeightOfBST(root.rightChild);
11+
12+
if (leftHeight > rightHeight) {
13+
return leftHeight + 1;
14+
}
15+
return rightHeight + 1;
16+
}
17+
18+
// create a BST
19+
// const myBST = new BST(6);
20+
// myBST.add(4);
21+
// myBST.add(9);
22+
// myBST.add(2);
23+
// myBST.add(5);
24+
// myBST.add(14);
25+
// myBST.add(8);
26+
// myBST.add(12);
27+
// myBST.add(10);
28+
29+
// // find 3rd max
30+
// // console.log(myBST.root);
31+
// console.log(myBST.traversePreorder());
32+
// // console.log(myBST.root.rightChild);
33+
// console.log(findHeightOfBST(myBST.root));
34+
35+
module.exports = findHeightOfBST;

0 commit comments

Comments
 (0)