-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (54 loc) · 1.05 KB
/
index.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
49
50
51
52
53
54
55
56
57
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
function maxDepth(root) {
//排除[] --> null空树
if (!root) {
return 0;
}
let depth = 0;
//Math.max([]) --> -Infinity错误
let arr = [0];
function getDepth(root, depth) {
//层级
depth++;
if (root.left) {
getDepth(root.left, depth);
}
if (root.right) {
getDepth(root.right, depth);
}
if (!root.left && !root.right) {
//保存每个叶子所在层级
arr.push(depth);
}
}
getDepth(root, depth);
return Math.max(...arr);
}
// 数组二叉树算法,不转换为对象
function getMaxDepth(root) {
let depth = 0;
let count = 0;
while (root.length) {
let val = Math.pow(2, count);
count = 0;
for (let j = 0; j < val; j++) {
if (root[j] !== null) {
count++;
}
}
depth++;
root.splice(0, val);
}
return depth;
}
module.exports = [getMaxDepth];