Skip to content

Commit 5c3d533

Browse files
authored
Merge pull request neetcode-gh#362 from Chrislee1996/main
Added 199-binary-tree-right-side-view Javascript Solution
2 parents 11839ef + 0432e3a commit 5c3d533

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[]}
12+
*/
13+
var rightSideView = function(root) {
14+
let result = []
15+
let queue = []
16+
17+
if (root === null) {
18+
return []
19+
}
20+
21+
queue.push(root)
22+
23+
while(queue.length > 0){
24+
let length = queue.length
25+
for (let i = 0 ; i < length ; i++) {
26+
let node = queue.shift()
27+
if (i === length - 1) {
28+
result.push(node.val)
29+
}
30+
if (node.left !== null) queue.push(node.left)
31+
if (node.right !== null) queue.push(node.right)
32+
}
33+
}
34+
return result
35+
};

0 commit comments

Comments
 (0)