File tree 5 files changed +108
-0
lines changed
algorithms/BinaryTreeRightSideView
5 files changed +108
-0
lines changed Original file line number Diff line number Diff line change @@ -251,6 +251,7 @@ All solutions will be accepted!
251
251
| 24| [ Swap Nodes In Pairs] ( https://leetcode-cn.com/problems/swap-nodes-in-pairs/description/ ) | [ java/py/js] ( ./algorithms/SwapNodesInPairs ) | Medium|
252
252
| 143| [ Reorder List] ( https://leetcode-cn.com/problems/reorder-list/description/ ) | [ java/py/js] ( ./algorithms/ReorderList ) | Medium|
253
253
| 515| [ Find Largest Value In Each Tree Row] ( https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ ) | [ java/py/js] ( ./algorithms/FindLargestValueInEachTreeRow ) | Medium|
254
+ | 199| [ Binary Tree Right Side View] ( https://leetcode-cn.com/problems/binary-tree-right-side-view/description/ ) | [ java/py/js] ( ./algorithms/BinaryTreeRightSideView ) | Medium|
254
255
255
256
# Database
256
257
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Binary Tree Right Side View
2
+ We can solve this problem by BFS
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ public List <Integer > rightSideView (TreeNode root ) {
12
+ List <Integer > res = new ArrayList <Integer >();
13
+ LinkedList <TreeNode > stack = new LinkedList <TreeNode >(),
14
+ nextStack = new LinkedList <TreeNode >();
15
+ LinkedList <Integer > values = new LinkedList <Integer >();
16
+
17
+ if (root != null ) stack .addFirst (root );
18
+
19
+ while (stack .size () > 0 ) {
20
+ TreeNode node = stack .removeLast ();
21
+ values .addFirst (node .val );
22
+
23
+ if (node .left != null ) nextStack .addFirst (node .left );
24
+ if (node .right != null ) nextStack .addFirst (node .right );
25
+
26
+ if (stack .size () == 0 ) {
27
+ res .add (values .get (0 ));
28
+ values = new LinkedList <Integer >();
29
+ stack = nextStack ;
30
+ nextStack = new LinkedList <TreeNode >();
31
+ }
32
+ }
33
+
34
+ return res ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change
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
+ var rightSideView = function ( root ) {
13
+ let res = [ ] ,
14
+ stack = [ ] ,
15
+ nextStack = [ ] ,
16
+ values = [ ]
17
+
18
+ if ( root ) stack . unshift ( root )
19
+
20
+ while ( stack . length > 0 ) {
21
+ let node = stack . pop ( )
22
+ values . unshift ( node . val )
23
+ if ( node . left ) nextStack . unshift ( node . left )
24
+ if ( node . right ) nextStack . unshift ( node . right )
25
+
26
+ if ( stack . length == 0 ) {
27
+ res . push ( values . shift ( ) )
28
+ values = [ ]
29
+ stack = nextStack
30
+ nextStack = [ ]
31
+ }
32
+ }
33
+
34
+ return res
35
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.left = None
6
+ # self.right = None
7
+
8
+ class Solution (object ):
9
+ def rightSideView (self , root ):
10
+ """
11
+ :type root: TreeNode
12
+ :rtype: List[int]
13
+ """
14
+ res = []
15
+ stack = []
16
+ next_stack = []
17
+ values = []
18
+
19
+ if root : stack .insert (0 , root )
20
+
21
+ while len (stack ) > 0 :
22
+ node = stack .pop ()
23
+ values .insert (0 , node .val )
24
+
25
+ if node .left : next_stack .insert (0 , node .left )
26
+ if node .right : next_stack .insert (0 , node .right )
27
+
28
+ if len (stack ) == 0 :
29
+ res .append (values [0 ])
30
+ values = []
31
+ stack = next_stack
32
+ next_stack = []
33
+
34
+ return res
You can’t perform that action at this time.
0 commit comments