-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111.js
More file actions
47 lines (44 loc) · 1.38 KB
/
Copy path111.js
File metadata and controls
47 lines (44 loc) · 1.38 KB
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
var minDepth = function(root) {
let depth = 0; //DFS
function findDepth(root,depth){
if(root == null) {
return depth;
}if(root.left == null){
return findDepth(root.right, depth +1);
}if(root.right == null){
return findDepth(root.left, depth+1);
}
return Math.min(findDepth(root.left, depth+1), findDepth(root.right, depth+1));
}
return findDepth(root,depth);
};
var minDepth2 = function(root) { //BFS
if(root == null) return 0;
let depth = 0;
let queue = [];
queue.push(root);
while(queue.length > 0){
let numberOfNodes = queue.length;
for(let i=0; i<numberOfNodes;i++){
let current = queue.shift();
if(current.left == null && current.right === null ){
depth++;
return depth;
}
if(current.left) queue.push(current.left);
if(current.right) queue.push(current.right);
}
depth++;
}
return depth;
};
var minDepth3 = function(root) { //BFS
let queue = [{node: root, level:1}];
if(root == null) return 0;
while(queue.length){
let {node, level} = queue.shift();
if(!node.left && !node.right) return level;
if(node.left) queue.push({node: node.left, level: level+1 });
if(node.right) queue.push({node: node.right, level: level+1})
}
};