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;
        }
    }
  • 相关阅读:
    python14 1.带参装饰器 | wrapper 了了解 # 2.迭代器 ***** # 可迭代对象 # 迭代器对象 # for迭代器 # 枚举对象
    python13 1.函数的嵌套定义 2.global、nonlocal关键字 3.闭包及闭包的运用场景 4.装饰器
    python12--字符串的比较 函数的默认值的细节 三元表达式 函数对象 名称空间 作用域 列表与字典的推导式 四则运算 函数的嵌套
    python11 函数的定义,调用,分类
    python10--函数的来源,优点,定义,组成,使用(定义,调用)函数的分类,函数的返回值
    python1--计算机原理 操作系统 进制 内存分布
    python2 配置环境变量
    python3 数据类型
    Java创建线程的三种方式
    webhooks动态更新配置
  • 原文地址:https://www.cnblogs.com/airwindow/p/4217573.html
Copyright © 2011-2022 走看看