-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumDepthOfBST.js
48 lines (36 loc) · 1.26 KB
/
MaximumDepthOfBST.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
// Actual Solution
let maxDepth = function(root) {
let maxDepth = 0;
function iterate(root, depth){
if(root == null)
return;
iterate(root.left, depth+1);
iterate(root.right, depth+1);
maxDepth = depth > maxDepth ? depth : maxDepth;
}
iterate(root, 1);
return maxDepth;
};
// Solution Ends
let insertToBst = (array, rootNode, position) =>{
if(position == array.length || rootNode.val == null){
return
}
let isGreater = array[position] > rootNode.val
if(isGreater && rootNode.right == null)
return rootNode.right = new TreeNode(array[position], null, null)
else if(!isGreater && rootNode.left == null)
return rootNode.left = new TreeNode(array[position], null, null)
isGreater ? insertToBst(array, rootNode.right, position) : insertToBst(array, rootNode.right, position)
}
let array = [3,9,20,15,7]
let rootNode = new TreeNode(array[0], null, null);
for(let i=1; i<array.length; i++){
insertToBst(array, rootNode, i);
}
console.log(maxDepth(rootNode));