-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path104_maxDepth.js
More file actions
49 lines (46 loc) · 1.01 KB
/
Copy path104_maxDepth.js
File metadata and controls
49 lines (46 loc) · 1.01 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
48
49
/**
* https://leetcode.cn/problems/maximum-depth-of-binary-tree/
* 二叉树的最大深度
* easy
*/
// 递归
var maxDepth = function (root) {
if (!root) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
// queue + BFS
var maxDepth = function (root) {
if (!root) return 0;
const queue = [root];
let count = 0;
while (queue.length) {
const size = queue.length;
for (let i = 0; i < size; ++i) {
const node = queue.pop();
if (node.left) queue.unshift(node.left);
if (node.right) queue.unshift(node.right);
}
++count;
}
return count;
}
// stack + dfs
var maxDepth = function (root) {
if (!root) return 0;
const stack = [root], level = [1];
let max = 0;
while (stack.length) {
const node = stack.pop();
const lv = level.pop();
max = Math.max(max, lv);
if (node.right) {
stack.push(node.right);
level.push(lv + 1);
}
if (node.left) {
stack.push(node.left);
level.push(lv + 1);
}
}
return max;
}