My java solution (1ms)


  • 0
    F
        public List<List<Integer>> findLeaves(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if(root == null) return res;
            getLeaves(res, root);
            return res;
        }
        private int getLeaves(List<List<Integer>> res, TreeNode n) {
            if(n == null) return -1;
            int h = Math.max(getLeaves(res, n.left), getLeaves(res, n.right)) + 1;
            if(h >= res.size()) res.add(new ArrayList<Integer>());
            res.get(h).add(n.val);
            return h;
        }
    }

Log in to reply
 

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