1ms traverse and buckets

  • 0

    put nodes in the bucket of distance to the leave, the distance to the leave is decided by the larger distance out of left distance and right distance.

    public class Solution {
        public List<List<Integer>> findLeaves(TreeNode root) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            trav(root, res);
            return res;
        private int trav(TreeNode node, List<List<Integer>> res) {
            if (node == null) {
                return 0;
            int ld = trav(node.left, res);
            int rd = trav(node.right, res);
            int depth = ld > rd ? ld : rd;
            if (depth >= res.size()) {
                List<Integer> tmp = new ArrayList<Integer>();
            } else {
            return depth+1;

Log in to reply

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