File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments