Skip to content

Commit 25f129b

Browse files
committed
Update 199. 二叉树的右视图
1 parent b460920 commit 25f129b

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

Diff for: algorithms/binary-tree-right-side-view.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@
77
* }
88
*/
99
/**
10+
* 199. 二叉树的右视图
1011
* @param {TreeNode} root
1112
* @return {number[]}
1213
*/
1314
var rightSideView = function (root) {
14-
// 思路:
15-
// 层序遍历的时候,判断是否遍历到单层的最后面的元素
16-
// 如果是,就放进result数组中,随后返回result就可以了
17-
1815
const result = [];
1916
const queue = [];
20-
queue.push(root);
21-
22-
if (!root) return [];
17+
root && queue.push(root);
2318

24-
while (queue.length > 0) {
19+
while (queue.length) {
2520
let size = queue.length;
21+
result.push(queue[0].val);
2622

2723
while (size--) {
2824
const node = queue.shift();
29-
30-
size === 0 && result.push(node.val);
31-
node.left && queue.push(node.left);
3225
node.right && queue.push(node.right);
26+
node.left && queue.push(node.left);
3327
}
3428
}
3529

0 commit comments

Comments
 (0)