-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path104.js
More file actions
46 lines (43 loc) · 985 Bytes
/
104.js
File metadata and controls
46 lines (43 loc) · 985 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if (root === null) return 0
let q = []
let res = 1
q.push(root)
while (q.length) {
let len = q.length
while (len) {
let node = q.shift()
node.left ? q.push(node.left) : ''
node.right ? q.push(node.right) : ''
len--
}
res++
}
return res
};
/*
2021/9/11
78 97
dfs遍历二叉树
const dfs = node => {
if (node === null) return 0
if (node.left === null && node.right === null) return 1
return Math.max(dfs(node.left), dfs(node.right)) + 1
}
return dfs(root)
2021/9/26
更新bfs
75 7
*/