zoukankan      html  css  js  c++  java
  • LeetCode 二叉树的锯齿形层次遍历

    第103题

    
    给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
    
    例如:
    给定二叉树 [3,9,20,null,null,15,7],
    
        3
       / 
      9  20
        /  
       15   7
    返回锯齿形层次遍历如下:
    
    [
      [3],
      [20,9],
      [15,7]
    ]
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal
    
    

    解题思路

    基于二叉树层序遍历改一点代码即可.二叉树层序遍历

    • 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的抽象数据类型。双端队列中的元素可以从两端弹出,插入和删除操作限定在队列的两边进行。
    • 本题和普通的层序遍历区别在于如何正确的选取加入子节点的顺序以及先后.
    • 利用双端队列,左进的时候,右出;右进的时候,左出。正好满足一层正序遍历,一层逆序遍历。

    代码实现

    class Solution103_1 {
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            if (root == null) {
                return new ArrayList<>();
            }
            return BFS(root);
        }
    
        List<List<Integer>> BFS(TreeNode root) {
            Deque<TreeNode> deque = new LinkedList<>();
            deque.addLast(root);
            List<List<Integer>> result = new LinkedList<>();
            boolean reverse = true;
            while (!deque.isEmpty()) {
                int size = deque.size();
                List<Integer> subResult = new LinkedList<>();
                for (int i = 0; i < size; i++) {
                    TreeNode node;
                    if (reverse) {
                        //头出
                        node = deque.pollFirst();
    
                        //尾进
                        if (node.left != null) {
                            deque.addLast(node.left);
                        }
                        if (node.right != null) {
                            deque.addLast(node.right);
                        }
                    } else {
                        //尾出
                        node = deque.pollLast();
    
                        //头进
                        if (node.right != null) {
                            deque.addFirst(node.right);
                        }
                        if (node.left != null) {
                            deque.addFirst(node.left);
                        }
                    }
                    subResult.add(node.val);
                }
                result.add(subResult);
                reverse = !reverse;
            }
            return result;
        }
    }
    

    完整代码

    public class Sub103 {
        public static void main(String[] args) {
            TreeNode root = new TreeNode(3);
            root.left = new TreeNode(9);
            root.right = new TreeNode(20);
            root.right.left = new TreeNode(15);
            root.right.right = new TreeNode(7);
    
    //        TreeNode root = new TreeNode(1);
    //        root.left = new TreeNode(2);
    //        root.right = new TreeNode(3);
    //        root.left.left = new TreeNode(4);
    //        root.right.right = new TreeNode(5);
    
            Solution103_1 solution = new Solution103_1();
            List<List<Integer>> list = solution.zigzagLevelOrder(root);
            for (List<Integer> subList : list) {
                System.out.println(subList.toString());
            }
        }
    }
    
    class Solution103_1 {
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            if (root == null) {
                return new ArrayList<>();
            }
            return BFS(root);
        }
    
        List<List<Integer>> BFS(TreeNode root) {
            Deque<TreeNode> deque = new LinkedList<>();
            deque.addLast(root);
            List<List<Integer>> result = new LinkedList<>();
            boolean reverse = true;
            while (!deque.isEmpty()) {
                int size = deque.size();
                List<Integer> subResult = new LinkedList<>();
                for (int i = 0; i < size; i++) {
                    TreeNode node;
                    if (reverse) {
                        //头出
                        node = deque.pollFirst();
    
                        //尾进
                        if (node.left != null) {
                            deque.addLast(node.left);
                        }
                        if (node.right != null) {
                            deque.addLast(node.right);
                        }
                    } else {
                        //尾出
                        node = deque.pollLast();
    
                        //头进
                        if (node.right != null) {
                            deque.addFirst(node.right);
                        }
                        if (node.left != null) {
                            deque.addFirst(node.left);
                        }
                    }
                    subResult.add(node.val);
                }
                result.add(subResult);
                reverse = !reverse;
            }
            return result;
        }
    }
    

    资料

  • 相关阅读:
    OpenStack local.conf
    Murano Weekly Meeting 2015.07.21
    Python package和folder
    WSGI学习系列eventlet.wsgi
    OpenStack Weekly Rank 2015.07.20
    Eventlet Greenlet
    OpenStack Weekly Meeting 2015.07.17
    OpenStack创建实例错误解决方法
    Linux Shell命令系列(5) VI编辑器
    linux统计使用最多的10个命令
  • 原文地址:https://www.cnblogs.com/tqlin/p/11837628.html
Copyright © 2011-2022 走看看