-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102_javascript.js
More file actions
81 lines (72 loc) · 1.61 KB
/
102_javascript.js
File metadata and controls
81 lines (72 loc) · 1.61 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
// 示例:
// 二叉树:[3,9,20,null,null,15,7],
// 3
// / \
// 9 20
// / \
// 15 7
// 返回其层次遍历结果:
// [
// [3],
// [9,20],
// [15,7]
// ]
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
// BFS
var levelOrder = function(root) {
if(!root) return []
let tempArr = [root];
let res = [];
while(tempArr.length){
let length = tempArr.length;
let resArr = [];
while(length --){
let temp = tempArr.shift();
resArr.push(temp.val);
if(temp.left){
tempArr.push(temp.left);
}
if(temp.right){
tempArr.push(temp.right);
}
}
res.push(resArr)
}
return res
};
// DFS
var levelOrder = function(root) {
if(!root) return []
let res = [];
let dfs = (node, depth) =>{
// 当前层数加一 后 res数组push一个新数组
if(res.length < depth){
res.push([])
}
// depth -1 而非res.length -1
// 后者在深度搜索时 会有问题
// 而前者 为当前循环中的值depth值
res[depth-1].push(node.val);
if(node.left){
// 当前层数加一
dfs(node.left,depth+1);
}
if(node.right){
// 当前层数加一
dfs(node.right,depth+1);
}
}
dfs(root,1);
return res;
};