Java solution of 10 lines code(DFS)


  • 6
    W
    public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        help(root,1,result);
        return result;
    }
    public void help(TreeNode root, int depth, List<Integer> result){
        if(root==null) return;
        if(result.size()<depth) result.add(root.val);
        help(root.right,depth+1,result);
        help(root.left,depth+1,result);
    }
    

    }


Log in to reply
 

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