java with dfs


  • 0
    3
    List<Integer> list = new ArrayList<Integer>();
    	public List<Integer> rightSideView(TreeNode root) {
    		list.clear();
    		_rightSideView(root, 0);
    		return list;
    	}
    	
    	private void _rightSideView(TreeNode root, int level){
    		if(root == null) return;
    		
    		if(list.size() <= level){
    			list.add(root.val);
    		}else{
    			list.set(level, root.val);
    		}
    		
    		_rightSideView(root.left, level+1);
    		_rightSideView(root.right, level+1);
    	}
    

Log in to reply
 

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