JAVA divide and conquer solution


  • 1
    L
    public class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> r = new ArrayList<Integer>();
            if(root==null){
                return r;
            }
            r.add(root.val);
            List<Integer> rt = rightSideView(root.right);
            List<Integer> lt = rightSideView(root.left);
            for(int i=rt.size();i<lt.size();i++){
                rt.add(lt.get(i));
            }    
            r.addAll(rt);
            return r;
        }
    }
    

    I divide this problem into two parts : solving the right tree side view and left tree side view ,and then merge
    the results


Log in to reply
 

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