2ms Java recursive solution. Easy to understand.


  • 1
    C

    Merging the overflowed left subtree ride side view into right side subtree right side view.

    public List<Integer> rightSideView(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            if(root == null) return result;
            result.add(root.val);
            List<Integer> rsvl = rightSideView(root.left);
            List<Integer> rsvr = rightSideView(root.right);
            result.addAll(rsvr);
            if(rsvl.size() > rsvr.size()){
                for(int i = rsvr.size(); i< rsvl.size(); i++){
                    result.add(rsvl.get(i));
                }
            }
            return result;
        }

Log in to reply
 

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