zoukankan      html  css  js  c++  java
  • [LeetCode#103]Binary Tree Zigzag Level Order Traversal

    The problem:

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

    For example:
    Given binary tree {3,9,20,#,#,15,7},

        3
       / 
      9  20
        /  
       15   7
    

    return its zigzag level order traversal as:

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

    Soltuion 1:

    public class Solution {
        public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
            
            ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>> ();
            if (root == null)
                return ret;
            
            TreeNode cur_node;
            ArrayList<Integer> item = new ArrayList<Integer> ();
            Queue<TreeNode> queue = new LinkedList<TreeNode> ();//Queue is abstract class!
            queue.offer(root);
            int cur_num = 1;
            int next_num = 0;
            
            while (queue.size() > 0) {
                cur_node = queue.poll();
                item.add(cur_node.val);
                cur_num --;
                
                if (cur_node.left != null) {
                    queue.offer(cur_node.left);
                    next_num ++;
                }
                
                if (cur_node.right != null) {
                    queue.offer(cur_node.right);
                    next_num ++;
                }
                
                if (cur_num == 0) {
                    ret.add(item);
                    cur_num = next_num;
                    next_num = 0;
                    item = new ArrayList<Integer> ();
                }
            }
                        
            for (int i = 0; i < ret.size(); i++) {
                if (i % 2 == 1)
                    Collections.reverse(ret.get(i));
            }
            
            return ret;
        }
    }

    My analysis for solution2:

    The problem could also use two stacks to solve. The main ideas of using two stacks are following:
    1. use two stacks, one stack for odd level and one stack for even level. since the odd levels and even levels interleave with each other, we don't need two worry one level's node would mixed with another level's node.
    Stack<TreeNode> stack1 = new Stack<TreeNode> ();
    Stack<TreeNode> stack2 = new Stack<TreeNode> ();
    
    2. we use a cur_level to indicate which the level we are currently in. 
    int cur_level = 0;
    we update it when we about to enter the new level.
    if (cur_num == 0) {
        ret.add(item);
        cur_num = next_num;
        next_num = 0;
        cur_level ++;
        item = new ArrayList<Integer> ();
    }
    
    3. to reach the goal of ZigZag traversal, 
       3.1 when we are in odd level, we add the node's child from left to right. (stack would reverse it)
       3.2 when we are in even level, we add the node's child from right to left. (stack would reverse it)

    My solution2:

    public class Solution {
        public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
            
            ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>> ();
            if (root == null)
                return ret;
                
            TreeNode cur_node;
            ArrayList<Integer> item = new ArrayList<Integer> ();
            Stack<TreeNode> stack1 = new Stack<TreeNode> ();
            Stack<TreeNode> stack2 = new Stack<TreeNode> ();
            
            int cur_num = 1;
            int next_num = 0;
            int cur_level = 0;
            stack1.push(root);
            
            while (stack1.size() != 0 || stack2.size() != 0) {
                
                if (cur_level % 2 == 0) {
                    cur_node = stack1.pop();
                    item.add(cur_node.val);
                    cur_num --;
                    if (cur_node.left != null) { //add the next level's node into stack2 (from left to right)
                        stack2.push(cur_node.left);
                        next_num ++;
                    }
                    if (cur_node.right != null) {
                        stack2.push(cur_node.right);
                        next_num ++;
                    }
                    
                } else {
                    
                    cur_node = stack2.pop();
                    item.add(cur_node.val);
                    cur_num --;
                    if (cur_node.right != null) {//add the next level's node into stack1 (from right to left)
                        stack1.push(cur_node.right);
                        next_num ++;
                    }
                    if (cur_node.left != null) {
                        stack1.push(cur_node.left);
                        next_num ++;
                    }
                }
                
                if (cur_num == 0) {
                    ret.add(item);
                    cur_num = next_num;
                    next_num = 0;
                    cur_level ++;
                    item = new ArrayList<Integer> ();
                }
            }
            return ret;
        }
    }
  • 相关阅读:
    shell 从键盘读取输入时删除输入的字符
    spring boot 下 mapper接口与xml文件映射问题
    tcpdump
    Linux TCP自连接问题
    git commit 合并到指定分支
    工作教训总结
    git 撤销修改和版本回退
    Java 注解方式校验请求参数
    JVM 监控工具——jconsole
    排查Full GC
  • 原文地址:https://www.cnblogs.com/airwindow/p/4217573.html
Copyright © 2011-2022 走看看