My 2ms Java Solution


  • 1
    A
    public List<Integer> rightSideView(TreeNode root) {
            ArrayList<Integer> result = new ArrayList<Integer>();
            int[] prevLevel = new int[1];
            traverse(root,result,0,prevLevel);
            return result;
        }
        public void traverse(TreeNode root,List<Integer> result,int level,int[] prevLevel){
            if(root == null) return;
            level = level+1;
            if(level > prevLevel[0]){ result.add(root.val); prevLevel[0] =level;}
            traverse(root.right,result,level,prevLevel);
            traverse(root.left,result,level,prevLevel);
        }

Log in to reply
 

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