Skip to content

Commit d0d4ad2

Browse files
authored
Create 199-binary-tree-right-side-view.js
1 parent 356e574 commit d0d4ad2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

199-binary-tree-right-side-view.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[]}
11+
*/
12+
const rightSideView = function(root) {
13+
if(root == null) return []
14+
const queue = [root]
15+
const res = []
16+
while(queue.length) {
17+
const len = queue.length
18+
for(let i = 0; i < len; i++) {
19+
const el = queue.shift()
20+
if(i === len - 1) res.push(el.val)
21+
if(el.left) queue.push(el.left)
22+
if(el.right) queue.push(el.right)
23+
}
24+
}
25+
return res
26+
};

0 commit comments

Comments
 (0)