北邮复试刷题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;
    }
}
相关推荐
wregjru18 小时前
【MySQL】5. 数据更新与查询详解
java·数据库·mysql
五阿哥永琪18 小时前
java8新特性 时间间隔类 Duration和Period
java
.豆鲨包18 小时前
【Android】HttpURLConnection解析
android·java
闻哥18 小时前
Docker Swarm 负载均衡深度解析:VIP vs DNSRR 模式详解
java·运维·jvm·docker·容器·负载均衡
panzer_maus18 小时前
工厂模式、代理模式与单例模式的介绍
java·设计模式·代理模式
小林学编程18 小时前
模型上下文协议(MCP)的理解
java·后端·llm·prompt·resource·tool·mcp协议
软泡芙19 小时前
【Bug】ReactiveUI WPF绑定中依赖属性不更新的问题分析与解决方案
java·bug·wpf
小程故事多_8019 小时前
Harness实战指南,在Java Spring Boot项目中规范落地OpenSpec+Claude Code
java·人工智能·spring boot·架构·aigc·ai编程
浪扼飞舟19 小时前
WPF输入验证(ValidationRule)
java·javascript·wpf
砍材农夫1 天前
spring-ai 第四多模态API
java·人工智能·spring