107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]

Solution1:BFS

思路:BFS + 每行cur_result 插入LinkedList<list>到头
Time Complexity: O(N) Space Complexity: O(N)

Solution2:pre-order DFS with level [LinkedList]

思路:DFS过程中不同level的结果list 保存到 对应LinkedList<list> result[level]中。
时间复杂度是O(N^2): 因为当result是LinkedList时,虽然扩大创建时的result.add(0, new LinkedList<Integer>())是O(1),但get到result[level]位置需要O(n);但如果result用ArrayList时get虽然get到result[level]位置是O(1),但扩大创建时的result.add(0, new LinkedList<Integer>())may cost的移动是O(n);

Time Complexity: O(N^2) Space Complexity: O(N) 递归缓存

则可以Solution3 提前用O(N)求得max_height,先建立好ArrayList的result_list,这样就不需要扩大创建(may cost 移动O(n))

Solution3:pre-order DFS with level [prebuild-arraylist]

思路:提前求得max_height并创建好ArrayList<list> result, DFS过程中不同level的结果list 保存到 对应 result[level]中
Time Complexity: O(N) Space Complexity: O(N)

Solution1 Code:

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        
        if(root == null) return result;
        
        queue.offer(root);
        while(!queue.isEmpty()) {
            int num_on_level = queue.size();
            List<Integer> cur_result = new LinkedList<Integer>();
            for(int i = 0; i < num_on_level; i++) {
                TreeNode cur = queue.poll();
                if(cur.left != null) queue.add(cur.left);
                if(cur.right != null) queue.add(cur.right);
                cur_result.add(cur.val);
            }
            result.add(0, cur_result);
        }
        return result;
    }
}

Solution2 Code:

class Solution2 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        dfs(result, root, 0);
        return result;
    }
    
    public void dfs(List<List<Integer>> result, TreeNode root, int level) {
        if (root == null) return;
        if (level == result.size()) {
            result.add(0, new LinkedList<Integer>());
        }
        result.get(result.size() - level - 1).add(root.val);
        dfs(result, root.left, level+1);
        dfs(result, root.right, level+1);
    }
}

Solution3 Code:

class Solution3 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        int total_levels = maxHeight(root);
        System.out.println("total_levels: " + total_levels);
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for(int i = 0; i < total_levels; i++) {
            result.add(new LinkedList<Integer>());
        }
        
        dfs(result, root, 0);
        return result;
    }
    
    public void dfs(List<List<Integer>> result, TreeNode root, int level) {
        if (root == null) return;
        System.out.println("level: " + level);
        result.get(result.size() - level - 1).add(root.val);
        dfs(result, root.left, level + 1);
        dfs(result, root.right, level + 1);
    }
    
    private int maxHeight(TreeNode root) {
        if(root == null) return 0;  
        return 1 + Math.max(maxHeight(root.left), maxHeight(root.right));
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容