LeetCode高频题:二叉树的锯齿形(Z字形,之字形)层序遍历

LeetCode高频题:二叉树的锯齿形(Z字形,之字形)层序遍历

提示:本题是系列LeetCode的150道高频题,你未来遇到的互联网大厂的笔试和面试考题,基本都是从这上面改编而来的题目
互联网大厂们在公司养了一大批ACM竞赛的大佬们,吃完饭就是设计考题,然后去考应聘人员,你要做的就是学基础树结构与算法,然后打通任督二脉,以应对波云诡谲的大厂笔试面试题!
你要是不扎实学习数据结构与算法,好好动手手撕代码,锻炼解题能力,你可能会在笔试面试过程中,连题目都看不懂!比如华为,字节啥的,足够让你读不懂题
在这里插入图片描述


题目

给定一个二叉树,返回该二叉树的之字形层序遍历,
第一层从左往右,下一层从右向左,一直这样交替
数据返回:0<=n<=1500,树上每个节点的val满足|val|<=1500
要求,空间复杂度o(n),时间复杂度o(n)

LeetCode本题连接,LeetCode那个你看看题目就行
写代码按我的写,不要装进list,因为互联网长达的笔试不会让你装list
而是直接打印序列


一、审题

示例:
在这里插入图片描述


普通二叉树BFS按层遍历,每次都是从左往右打印

平时,正常层序遍历的话,就是队列依次加节点,节点的左右子不空加就行
【1】二叉树的宽度优先遍历BFS:按层的遍历方式,请你用队列实现DFS,或者请你用栈实现BFS

当时层序遍历是BFS搞定的,一个队列不断加左右子:

public static class Node{
        public int value;
        public Node left;
        public Node right;
        public Node(int v){
            value = v;
        }
    }

    //构造一颗树,今后方便使用
    public static Node generateBinaryTree(){
        //树长啥样呢
        //          1
        //        2   3
        //       4 5 6 7
        Node head = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        head.left = n2;
        head.right = n3;
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        n2.left = n4;
        n2.right = n5;
        Node n6 = new Node(6);
        Node n7 = new Node(7);
        n3.left = n6;
        n3.right = n7;
        return head;
    }

//复习:这样做,最开始将head加入队列
    //开始循环操作:弹出打印。
    //	然后看其左右子是否不为null,是就加入队列,否则不管。
    public static void bfsBinaryTreePrint(Node head){
        if (head == null) return;

        LinkedList<Node> queue = new LinkedList<>();
        queue.add(head);
        while (!queue.isEmpty()){
            Node cur = queue.poll();
            System.out.print(cur.value + " ");

            //按层加入左右子
            if (cur.left != null) queue.add(cur.left);
            if (cur.right != null) queue.add(cur.right);
        }
        //图也是这么干BFS,DFS就是用栈,一条道走到黑
    }

    public static void test(){
        Node cur = generateBinaryTree();
        BreadthFirstSearch(cur);
        bfsBinaryTreePrint(cur);
    }

    public static void main(String[] args){
        test();
    }


之字形遍历就和上面一点点的区别,直接用上面代码改编就行

这个之字形,实际上还是层序遍历的框架,唯一不同的是:
(1)当你在第1层遍历时,加的顺序应该是右子,左子,从右往左加,这样下次打印才是从右往左打印的
(2)当你在第2层遍历时,加的顺序应该是左子,右子,从左往右加,这样下次打印才是从左往右打印的
……
依次类推

你在奇数层,则加的顺序是右子左子
你在偶数层,则加的顺序是左子右子
在这里插入图片描述
整个思想超级简单
咱们直接改编BFS普通层序遍历(从左往右)代码来玩:

        //二叉树之字形遍历
    public static List<List<Integer>> bfsZigzagPrint(TreeNode head){
        if (head == null) return null;

        LinkedList<TreeNode> queue = new LinkedList<>();//BFS的重要结构
        queue.add(head);
        //用一个哈希表记录二叉树每个节点的层数
        HashMap<TreeNode, Integer> map = new HashMap<>();//node--层
        map.put(head, 1);//奇数层

        List<List<Integer>> ans = new ArrayList<>();//结果

        while (!queue.isEmpty()){
            //奇数层从尾部插入,偶数层从头插入
            List<Integer> tmp = new ArrayList<>();//单独处理一个层的那些节点
            int size = queue.size();//目前这一串——全部处理完,咱们才能接着处理别的
            
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                int level = map.get(cur);//拿到层数
//                System.out.print(cur.value + " ");//弹出打印cur.val
                //按层——看层数是奇数还是偶数
                if ((level & 1) == 1) {//奇数层,只能从右往左加——放tmp的屁股
                    tmp.add(cur.value);
                }else {//偶数层,从左往右家,放tmp头部
                    tmp.add(0, cur.value);
                }

                //正常放节点到队列
                if (cur.left != null) {
                    queue.add(cur.left);
                    map.put(cur.left, level + 1);
                }
                if (cur.right != null) {
                    queue.add(cur.right);
                    map.put(cur.right, level + 1);
                }
            }
            ans.add(tmp);//每次队列中一串搞定,tmp放ans
        }
        //图也是这么干BFS,DFS就是用栈,一条道走到黑

        return ans;
    }

代码完全就是普通bfs改编来的哦

测试:

    public static void test(){
        TreeNode cur = generateBinaryTree();
        BreadthFirstSearch(cur);
        bfsBinaryTreePrint(cur);
        System.out.println();
        //树长啥样呢
        //          1
        //        2   3
        //       4 5 6 7
        TreeNode cur2 = generateBinaryTree();
        List<List<Integer>> ans = bfsZigzagPrint(cur2);
        for(List<Integer> list : ans){
            for(Integer i : list) System.out.print(i + " ");
        }
    }

    public static void main(String[] args){
        test();
    }

结果:

1 2 3 4 5 6 7 
1 2 3 4 5 6 7 
1 3 2 4 5 6 7 

第三行就是zigzag打印
之字形层序遍历

LeetCode那个题目很狗
它要你把结果,按层放入列表,算了吧
咱们考题,会让你直接打印序列
就像我这样就行,很简单的

本题到此OK了

还是测一下leetcode:

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();//结果
        if (root == null) return ans;

        LinkedList<TreeNode> queue = new LinkedList<>();//BFS的重要结构
        queue.add(root);
        //用一个哈希表记录二叉树每个节点的层数
        HashMap<TreeNode, Integer> map = new HashMap<>();//node--层
        map.put(root, 1);//奇数层

        while (!queue.isEmpty()){
            //奇数层从尾部插入,偶数层从头插入
            List<Integer> tmp = new ArrayList<>();//单独处理一个层的那些节点
            int size = queue.size();//目前这一串——全部处理完,咱们才能接着处理别的

            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                int level = map.get(cur);//拿到层数
//                System.out.print(cur.value + " ");//弹出打印cur.val
                //按层——看层数是奇数还是偶数
                if ((level & 1) == 1) {//奇数层,只能从右往左加——放tmp的屁股
                    tmp.add(cur.val);
                }else {//偶数层,从左往右家,放tmp头部
                    tmp.add(0, cur.val);
                }

                //正常放节点到队列
                if (cur.left != null) {
                    queue.add(cur.left);
                    map.put(cur.left, level + 1);
                }
                if (cur.right != null) {
                    queue.add(cur.right);
                    map.put(cur.right, level + 1);
                }
            }
            ans.add(tmp);//每次队列中一串搞定,tmp放ans
        }
        //图也是这么干BFS,DFS就是用栈,一条道走到黑

        return ans;
    }
}

结果:
在这里插入图片描述

在这里插入图片描述


总结

提示:重要经验:

1)普通的bfs二叉树层序遍历,非常简单,字节将队列不断从左往右加就行了
2)本题就是从1)改变来的,bfs队列控制,奇数层的时候,让加入队列的顺序从右往左,偶数层的时候,加入队列从左往右
3)笔试求AC,可以不考虑空间复杂度,但是面试既要考虑时间复杂度最优,也要考虑空间复杂度最优。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
锯齿形层次遍历是一种二叉树的层次遍历方法,它会按照"之"字形的顺序遍历二叉树的节点。具体实现可以使用队列或者递归的方式来实现。 使用队列的方法可以按照层次遍历的思路来实现,首先将根节点加入队列中,然后依次取出队列中的节点。在每一层遍历时,我们可以使用一个临时列表来存储当前层的节点值,并将下一层的节点按照遍历顺序添加到新的队列中。同时,我们可以使用一个变量来记录当前层的层数,如果层数为偶数,则按照顺序添加到临时列表中;如果层数为奇数,则按照逆序添加到临时列表中。最后将临时列表添加到最终结果中。这样就能实现二叉树锯齿形层次遍历。 另一种实现方式是使用递归来进行遍历。我们可以使用一个辅助函数来实现递归遍历。在遍历过程中,我们可以传递当前节点和当前层的层数作为参数,并根据层数的奇偶性来决定节点值的添加位置。同时,我们可以使用一个列表来存储每一层的节点值,并在递归的最后将列表转换为最终的结果。这样也能实现二叉树锯齿形层次遍历。 综上所述,二叉树锯齿形层次遍历可以使用队列或递归来实现,具体取决于个人的喜好和代码风格。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [二叉树锯齿形层序遍历[分层遍历方式之一 -> 前序遍历+level]](https://blog.csdn.net/qq_43164662/article/details/125512062)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【LeetCode】103. 二叉树锯齿形层次遍历](https://blog.csdn.net/weixin_41888257/article/details/107284460)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰露可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值