Easy recursive Java Sol


  • 0
    V
        List<List<Integer>> toRet;
        public List<List<Integer>> findLeaves(TreeNode root) 
        {
            toRet = new ArrayList<List<Integer>>();
            recMeth(root);
            return toRet;
        }
        int recMeth(TreeNode root)
        {
            if(root == null)
                return -1;
            int l = recMeth(root.left);
            int r = recMeth(root.right);
            int ind = Math.max(l, r) + 1;
            if(toRet.size() <= ind)
                toRet.add(new ArrayList<Integer>());
            toRet.get(ind).add(root.val);
            return ind;
        }

Log in to reply
 

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