MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / rightSideView

Method rightSideView

TreeRightViewBinaryTree.java:2–31  ·  view source on GitHub ↗
(TreeNode root)

Source from the content-addressed store, hash-verified

1class Solution {
2 public List<Integer> rightSideView(TreeNode root) {
3
4 // Your code here
5 List<Integer> list = new ArrayList<>();
6 if(root==null) return list;
7 Queue<TreeNode> queue = new LinkedList<>();
8 queue.add(root);
9 while(!queue.isEmpty())
10 {
11 int level = queue.size();
12 for(int i=0;i<level;i++)
13 {
14 if(queue.peek().left!=null)
15 {
16 queue.add(queue.peek().left);
17 }
18 if(queue.peek().right!=null)
19 {
20 queue.add(queue.peek().right);
21 }
22 if(i==level-1)
23 {
24 list.add(queue.peek().val);
25 }
26 queue.remove();
27 }
28 }
29 return list;
30
31 }
32}

Callers

nothing calls this directly

Calls 2

addMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected