-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathMaxDeep.js
56 lines (50 loc) · 1.05 KB
/
MaxDeep.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
/*
[
{
"id":"1",
"childrens":[{
"id":"12",
"childrens":[]
},{
"id":"13",
"childrens":[{
"id":"123",
"childrens":[]
}]
}]
},
{
"id":"2",
"childrens":[]
}
]
*/
function maxDeep(list) {
// boundary case
if (!list.length) return 0;
let res = 1;
// 栈法获取
list.forEach(item => {
let tmp = 1;
const { childrens } = item;
if (childrens.length) {
// 开栈
const stack = [];
childrens.forEach(child => {
tmp = 1;
stack.push(child);
while (stack.length) {
tmp++;
const { childrens } = stack.pop();
if (childrens.length) {
stack.push(...childrens);
}
}
});
if (tmp > res) {
res = tmp;
}
}
});
return res;
}