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 number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments