• 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;
        }
    }
  • 相关阅读:
    你真的会玩SQL吗?EXISTS和IN之间的区别
    大数据征信六大难题待解
    大数据征信六大难题待解
    大数据小白的一些浅见
    大数据小白的一些浅见
    spss命令数据整理中compute与record命令的区别
    spss命令数据整理中compute与record命令的区别
    R语言实现分层抽样(Stratified Sampling)以iris数据集为例
    R语言实现分层抽样(Stratified Sampling)以iris数据集为例
    ORA-24247: 网络訪问被訪问控制列表 (ACL) 拒绝
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3536936.html
Copyright © 2020-2023  润新知