Binary Tree Level Order Traversal 系列

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

BFS

Time Complexity
O(N)
Space Complexity
Extra Space required for Level Order Traversal is O(w) where w is maximum width of Binary Tree.

思路

最基础的BFS

代码

public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
    // write your code here
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    if(root == null) return res;
    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.offer(root);
    while(!queue.isEmpty()){
        int size = queue.size();
        ArrayList<Integer> list= new ArrayList<Integer>();
        for(int i = 0; i < size; i++){
            TreeNode cur = queue.poll();
            list.add(cur.val);
            if(cur.left != null) queue.offer(cur.left);
            if(cur.right != null) queue.offer(cur.right);
        }
        res.add(new ArrayList<Integer>(list));
    }
    return res;
}

DFS

Time Complexity
O(N)
Space Complexity
Depth First Traversals is O(h) where h is maximum height of Binary Tree.
O(logn)

思路

通过看层数的方法做DFS

代码

public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
    // write your code here
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    if(root == null) return res;
    dfs(root, 0, res);
    return res;
}

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

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).

思路

原来的基础上最后把res reverse一下

代码

public List<List<Integer>> levelOrderBottom(TreeNode root) {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    if(root == null) return res;
    Queue<TreeNode> q = new LinkedList<TreeNode>();
    q.offer(root);
    while(!q.isEmpty()){
        List<Integer> list = new ArrayList<Integer>();
        int size = q.size();
        for(int i = 0; i < size; i++){
            TreeNode cur = q.poll();
            list.add(cur.val);
            if(cur.left != null) q.offer(cur.left);
            if(cur.right != null) q.offer(cur.right);
        }
        res.add(list);
    }
    Collections.reverse(res);
    return res;
}

Binary Tree Zigzag Level Order Traversal

思路

奇数层还是按照从左到右的顺序打印,偶数层要用从右到左的方式打印,用一个level来看是奇数层还是偶数层, 如果是偶数层就reverse一下整个List

代码

public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
    // write your code here
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    if(root == null) return res;
    Queue<TreeNode> q = new LinkedList<TreeNode>();
    int level = 1, size;
    q.offer(root);
    while(!q.isEmpty()){
        size = q.size();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < size; i++){
            TreeNode cur = q.poll();
            if(cur.left != null) q.offer(cur.left);
            if(cur.right != null) q.offer(cur.right);
            list.add(cur.val);
        }
        if(level % 2 == 0) Collections.reverse(list);
        res.add(list);
        level++;
    }
    return res;
}

思路

用deque做,用一个level来记录是奇数层还是偶数层, 奇数层是正常的level的queue的顺序,偶数层要全部反一下

代码

public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    if(root == null) return res;
    Deque<TreeNode> dq = new LinkedList<TreeNode>();
    dq.offer(root);
    int level = 1;
    while(!dq.isEmpty()){
        int size = dq.size();
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < size; i++){
            if(level % 2 == 0){
                TreeNode cur = dq.pollLast();
                list.add(cur.val);
                if(cur.right != null) dq.offerFirst(cur.right);
                if(cur.left != null) dq.offerFirst(cur.left);
            }
            if(level % 2 != 0){
                TreeNode cur = dq.pollFirst();
                list.add(cur.val);
                if(cur.left != null){
                    dq.offerLast(cur.left);
                }
                if(cur.right != null){
                    dq.offerLast(cur.right);
                }
            }
        }
        res.add(new ArrayList<Integer>(list));
        level++;
    }
    return res;
}

annielulu
5 声望5 粉丝

下一篇 »
Sorting