• Binary Tree Zigzag Level Order Traversal


    [解题思路]

    二叉树的层序遍历通过使用queue来实现,

    上网搜索了下,发现该题可以使用栈来解决,通过分析执行结果确实是后进先出

    这里通过定义leftToRight来表示是从左到右还是从右到左

    从左到右:先加left后加right

    从右到左:先加right后加left

    orderlevel traversal 遍历最后每行添加到result前 判断是leftToRight 还是Right to Left

    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
            ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
            
            if(root == null)
                return result;
                
            LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
            queue.add(root);
            boolean leftToRight = true;
            int nextLevelCount = 1;
            
            while(!queue.isEmpty()){
                int curLevel = nextLevelCount;
                nextLevelCount = 0;
                ArrayList<Integer> lvl = new ArrayList<Integer>();
                
                for(int i = 0; i < curLevel; i++){
                    TreeNode tmp = queue.poll();
                    lvl.add(tmp.val);
                    if(tmp.left != null){
                        queue.add(tmp.left);
                        nextLevelCount++;
                    }
                    if(tmp.right != null){
                        queue.add(tmp.right);
                        nextLevelCount++;
                    }
                }
                if(leftToRight){
                    result.add(lvl);
                    leftToRight = !leftToRight;
                }else{
                    ArrayList<Integer> reverseLvl = new ArrayList<Integer>();
                    for(int i = lvl.size()-1; i >= 0; i--){
                        reverseLvl.add(lvl.get(i));
                    }
                    result.add(reverseLvl);
                    leftToRight = !leftToRight;
                }
                
            }
            return result;
        }
    }
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
            ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
            if(root == null){
                return result;            
            }
            Stack<TreeNode> curLvl = new Stack<TreeNode>();
            Stack<TreeNode> nextLvl = new Stack<TreeNode>();
            boolean leftToRight = true;
            curLvl.push(root);
            ArrayList<Integer> output = new ArrayList<Integer>();
            while(!curLvl.empty()){
                TreeNode curNode = curLvl.pop();
                output.add(curNode.val);
                if(leftToRight){
                    if(curNode.left != null){
                        nextLvl.add(curNode.left);
                    }
                    if(curNode.right != null){
                        nextLvl.add(curNode.right);
                    }
                } else{
                    if(curNode.right != null){
                        nextLvl.add(curNode.right);
                    }
                    if(curNode.left != null){
                        nextLvl.add(curNode.left);
                    }
                }
                if(curLvl.empty()){
                    result.add(output);
                    output = new ArrayList<Integer>();
                    leftToRight = !leftToRight;
                    curLvl.addAll(nextLvl);
                    nextLvl.clear();
                }
            }
            return result;
        }
    }
  • 相关阅读:
    【Unittest】unittest相关解说
    python中导入模块/包的几种方式
    工具网站
    mapstruct 入门指南
    springboot 全局异常处理器
    多线程读表-压缩成zip下载
    编译执行和解释执行的区别
    swagger常用注解说明
    springboot整合swagger
    springboot整合druid
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3536936.html
Copyright © 2020-2023  润新知