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;
        }
    }
  • 相关阅读:
    H5页面跳到安卓APP和iosAPP
    JS location.href传参及接受参数
    获取当前日期及对应星期
    前端获取当前一周时间 数组形式
    Java基础(四) Object 数组转成 String 数组
    定时任务cron表达式详解
    jquery如何删除数组中的一个元素?
    Mybatis Mapper.xml 需要查询返回List<String>
    oracle的 listagg() WITHIN GROUP () 行转列函数的使用
    如何修改Oracle中表的字段长度?
  • 原文地址:https://www.cnblogs.com/airwindow/p/4217573.html
Copyright © 2011-2022 走看看