My java solution (1ms)

  • 0
        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>());
            return h;

Log in to reply

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