-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path104.js
More file actions
32 lines (27 loc) · 745 Bytes
/
104.js
File metadata and controls
32 lines (27 loc) · 745 Bytes
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
// Depth first search
var maxDepth = function(root) {
var dfs = function(root){
if(root===null)return 0;
var leftDepth = 1+dfs(root.left);
var rightDepth = 1+dfs(root.right);
return leftDepth>rightDepth?leftDepth:rightDepth;
}
return dfs(root);
};
// Breath first search
var maxDepth = function(root) {
if(root===null)return 0;
var q=[];
q.push(root);
var depth=0;
while(q.length!==0){
depth++;
var levelNum = q.length;
for(var i=0;i<levelNum;i++){
var currNode = q.shift();
if(currNode.left!==null)q.push(currNode.left);
if(currNode.right!==null)q.push(currNode.right);
}
}
return depth;
};