-
Notifications
You must be signed in to change notification settings - Fork 0
/
199-二叉树的右视图.js
55 lines (49 loc) · 1.18 KB
/
199-二叉树的右视图.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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
/*
BFS
记录每层的最右边的节点
*/
var rightSideView = function(root) {
if(!root) return [];
let queue = [];
let ans = [];
queue.push(root);
while(queue.length != 0) {
let size = queue.length;
for(let i = 0; i < size; i++) { // 这里确定了每层节点的区间
if(i == size - 1) ans.push(queue[0].val);
let node = queue.shift();
if(node.left) queue.push(node.left);
if(node.right) queue.push(node.right);
}
}
return ans;
};
/*
DFS
按照 「根结点 -> 右子树 -> 左子树」 的顺序访问,就可以保证每层都是最先访问最右边的节点的。
*/
var rightSideView = function (root) {
let res = [];
dfs(root, 0, res);
return res;
}
var dfs = function(root, depth, res) {
if(!root) return;
if(res.length === depth) {
res.push(root.val);
}
depth++;
dfs(root.right, depth, res);
dfs(root.left, depth, res);
}