File tree Expand file tree Collapse file tree 1 file changed +18
-14
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +18
-14
lines changed Original file line number Diff line number Diff line change 23
23
You should return [1, 3, 4]. */
24
24
25
25
public class _199 {
26
- //Using BFS is pretty straightforward. But there might be a smarter way.
26
+
27
27
public List <Integer > rightSideView (TreeNode root ) {
28
- List <Integer > res = new ArrayList <Integer >();
29
- if (root == null ) return res ;
30
- Queue <TreeNode > q = new LinkedList <TreeNode >();
28
+ List <Integer > result = new ArrayList <>();
29
+ if (root == null ) return result ;
30
+ Queue <TreeNode > q = new LinkedList <>();
31
31
q .offer (root );
32
- while (!q .isEmpty ()){
33
- int currentSize = q .size ();
34
- int i = 0 ;
35
- TreeNode currentNode = null ;
36
- for (; i < currentSize ; i ++){
37
- currentNode = q .poll ();
38
- if (currentNode .left != null ) q .offer (currentNode .left );
39
- if (currentNode .right != null ) q .offer (currentNode .right );
32
+ while (!q .isEmpty ()) {
33
+ int size = q .size ();
34
+ for (int i = 0 ; i < size ; i ++) {
35
+ TreeNode curr = q .poll ();
36
+ if (i == size -1 ) {
37
+ result .add (curr .val );
38
+ }
39
+ if (curr .left != null ) {
40
+ q .offer (curr .left );
41
+ }
42
+ if (curr .right != null ) {
43
+ q .offer (curr .right );
44
+ }
40
45
}
41
- res .add (currentNode .val );
42
46
}
43
- return res ;
47
+ return result ;
44
48
}
45
49
46
50
public static void main (String ...strings ){
You can’t perform that action at this time.
0 commit comments