import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null)
return res;
Queue<TreeNode> current=new LinkedList<TreeNode>();
current.offer(root);
int last=0;
Queue<TreeNode> next=new LinkedList<TreeNode>();
while(!current.isEmpty()||!next.isEmpty())
{
if(!current.isEmpty())
{
TreeNode temp=current.poll();
last=temp.val;
if(temp.left!=null)
next.offer(temp.left);
if(temp.right!=null)
next.offer(temp.right);
}
else
{
res.add(last);
while(!next.isEmpty())
current.offer(next.poll());
}
}
res.add(last);
return res;
}
}