Monday, November 2, 2015

[leetcode] Binary Tree Level Order Traversal II

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Solution {
 List<List<Integer>> result = new ArrayList<List<Integer>>();
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        dfsLevel(root, 0);
        Collections.reverse(result);
        return result;
    }

    private void dfsLevel(TreeNode root, int level){
     if (root == null) return;
     if (result.size() < level+1) result.add(new ArrayList<Integer>());
     result.get(level).add(root.val);
     dfsLevel(root.left, level+1);
     dfsLevel(root.right, level+1);
    }
}

No comments:

Post a Comment