Java Easy BFS Solution.


  • 0
    S
        public List<Integer> rightSideView(TreeNode root) {
        	List<Integer> result = new LinkedList<>();
        	if(root==null) return result;
        	Queue<TreeNode> queue = new LinkedList<TreeNode>();
        	
        	queue.add(root);
        	while(!queue.isEmpty()){
        		int size = queue.size();
        		while(size-- > 0) {
        			TreeNode cur = queue.poll();
    				if(cur.left!=null) queue.add(cur.left);
    				if(cur.right!=null) queue.add(cur.right);
        			if(size==0) {
        				result.add(cur.val);
        			}
        		}
        	}
        	
        	return result;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.