My simple accepted solution(JAVA)


  • 0
    H
    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            if(root==null)
             return (new ArrayList<Integer>());
            if(root.left==null && root.right==null){
                List<Integer> list = new ArrayList<Integer>();
                list.add(root.val);
                return list;
            }
            List<Integer> left = rightSideView(root.left);
            List<Integer> right = rightSideView(root.right);
            if(right.size()<left.size()){
                for(int i=right.size();i<left.size();i++)
                  right.add(left.get(i));
            }
            right.add(0,root.val);
            return right;
        }
    }

Log in to reply
 

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