Very simple & short java solution 1ms


  • 0
    C
    public List<List<Integer>> findLeaves(TreeNode root) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            helper(root, result);
            return result;
        }
        private int helper(TreeNode root, List<List<Integer>> result){
            if(root == null) return 0;
            int layer = Math.max(helper(root.left, result), helper(root.right, result));
            if(result.size() == layer)
                result.add(new ArrayList<Integer>());
            result.get(layer).add(root.val);
            return layer+1;
        }
    

Log in to reply
 

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