北邮复试刷题103. 二叉树的锯齿形层序遍历

103. 二叉树的锯齿形层序遍历

给你二叉树的根节点 root ,返回其节点值的 锯齿形层序遍历 。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

xml 复制代码
示例 1:


输入:root = [3,9,20,null,null,15,7]
输出:[[3],[20,9],[15,7]]

示例 2:

输入:root = [1]
输出:[[1]]

示例 3:

输入:root = []
输出:[]

提示:

树中节点数目在范围 [0, 2000] 内
-100 <= Node.val <= 100

题解:

方法一:按层模拟BFS

java 复制代码
/**
 * 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 void reverse(List<Integer> list){
        int size = list.size();
        int tmp[] = new int[size];
        for(int i=0;i<size;i++){
            tmp[i] = list.get(i);
        }
        int index = 0;
        for(int i=size-1;i>=0;i--){
            list.set(index,tmp[i]);
            index++;
        }
    }

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        boolean flag = true; // true代表->   false代表<-
        List<Integer> first = new ArrayList<>();
        first.add(root.val);
        if(root.left != null)
            queue.offer(root.left);
        if(root.right != null)
            queue.offer(root.right);
        res.add(first);

        while(!queue.isEmpty()){
            List<Integer> tmp = new ArrayList<>();
            int count = queue.size();
            while(count > 0){
                TreeNode node = queue.poll();
                if(node.left != null)
                    queue.offer(node.left);
                if(node.right != null)
                    queue.offer(node.right);
                tmp.add(node.val);
                count--;
            }
            flag = !flag;
            if(!flag){
                //对此时取到的tmp顺序取反
                reverse(tmp);
            }

            res.add(tmp);
        }

        return res;
    }
}

方法二:双端队列+奇偶

java 复制代码
/**
 * 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>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        int len = 1;// 奇数代表->   偶数代表<-
        List<Integer> first = new LinkedList<>();
        first.add(root.val);
        if(root.left != null)
            queue.offer(root.left);
        if(root.right != null)
            queue.offer(root.right);
        res.add(first);
        len++;

        while(!queue.isEmpty()){
            // 队列依旧是传统队列,但是每一个加入到res中的小list都是用双端形式,从而形式上实现双端队列
            List<Integer> tmp = new LinkedList<>();
            // 也是因为链表形式相较于数组形式更利于反转
            int count = queue.size();
            while(count > 0){
                TreeNode node = queue.poll();
                if(node.left != null)
                    queue.add(node.left);      
                if(node.right != null)
                    queue.offer(node.right);
                if(len % 2 == 0){
                    tmp.addFirst(node.val); 
                }
                else{
                    tmp.addLast(node.val);
                }
                count--;
            }
            res.add(tmp);
            len++;
        }

        return res;
    }
}
相关推荐
二哈赛车手6 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
栗子~~6 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
YDS8296 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
未若君雅裁8 小时前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
AI人工智能+电脑小能手8 小时前
【大白话说Java面试题 第65题】【JVM篇】第25题:谈谈对 OOM 的认识
java·开发语言·jvm
阿维的博客日记9 小时前
Nacos 为什么能让配置动态生效?(涉及 @RefreshScope 注解)
java·spring
雨辰AI9 小时前
SpringBoot3 + 人大金仓读写分离 + 分库分表 + 集群高可用 全栈实战
java·数据库·mysql·政务
辰海Coding10 小时前
MiniSpring框架学习-完成的 IoC 容器
java·spring boot·学习·架构
小小编程路10 小时前
C++ 多线程与并发
java·jvm·c++
AI视觉网奇10 小时前
linux 检索库 判断库是否支持
java·linux·服务器