Clear Accepted java solution


  • 0
    N
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            if(root==null){
    				return new ArrayList<Integer>();
    			}
            List<TreeNode> next_queue = new ArrayList<TreeNode>();
            next_queue.add(root);
            return lookFromRight(next_queue);
        }
        
          public List<Integer> lookFromRight(List<TreeNode> queue){
    	        	List<TreeNode> next_queue = new ArrayList<TreeNode>();
    	        	
    	        	if(queue==null||queue.size()==0){
    	        		return new ArrayList<Integer>();
    	        	}
    	        	///add the first element
    	        	List<Integer> list = new ArrayList<Integer>();
    	        	list.add(queue.get(0).val);
    	        	
    	        	///add the next depth level node to the queue
    	        	int length = queue.size();
    	        	for(int i=0;i<length;i++){
    	        		if(queue.get(i).right!=null){
    	        			next_queue.add(queue.get(i).right);
    	        		}
    	        		if(queue.get(i).left!=null){
    	        			next_queue.add(queue.get(i).left);
    	        		}
    	        		    		
    	        	}
    	        	///recursive call
    	        	list.addAll(lookFromRight(next_queue));
    	        	
    	        	return list;   
    	        }
        
    }

Log in to reply
 

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