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

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

示例 1:

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

输出:[[3],[20,9],[15,7]]

示例 2:

输入:root = [1]

输出:[[1]]

示例 3:

输入:root = []

输出:[]


解决思路:

使用两个栈来实现功能,一个全局栈global,一个局部栈temp,此外设置一个标志位label来判断在每一层时保存孩子的顺序

使用全局栈来进行遍历输出节点,在输出的同时将左右孩子节点存入局部栈中。

当每一层遍历完毕之后,将局部栈克隆给全局栈,此时全局栈具有下一层的节点。

每当遍历一层之后,更换保存孩子的顺序,如下图所示:

由于需要遍历节点,所以时间复杂度为O(n)
代码如下:

java 复制代码
package 二叉树;

import java.util.*;

public class LeetCode103二叉树的锯齿层序遍历 {

    public static 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;
        }
    }

    public static void main(String[] args){
        TreeNode root=new  TreeNode(3);
        TreeNode node1=new  TreeNode(9);
        TreeNode node2=new  TreeNode(20);
        TreeNode node3=new  TreeNode(15);
        TreeNode node4=new  TreeNode(7);
        TreeNode node5=new  TreeNode(2);
        TreeNode node6=new  TreeNode(1);
        root.left=node1;
        root.right=node2;
        node2.left=node3;
        node2.right=node4;
        node1.right=node5;
        node1.left=node6;
        //定义层序遍历
        List<List<Integer>> result=new ArrayList<>();
        //使用栈
        Stack<TreeNode> global=new Stack<>();
        global.push(root);
        int label=0;
        while(!global.isEmpty()){
            int size=global.size();
            List<Integer> temp_list=new ArrayList<>();
            Stack<TreeNode> temp_stack=new Stack<>();
            for(int i=0;i<size;i++){
                TreeNode child=global.peek();
                global.pop();
                temp_list.add(child.val);
                if(label==0){
                    if(child.left!=null) temp_stack.push(child.left);
                    if(child.right!=null) temp_stack.push(child.right);
                }else{
                    if(child.right!=null) temp_stack.push(child.right);
                    if(child.left!=null) temp_stack.push(child.left);
                }
            }
            //将temp_stack中的对象克隆给global
            global= (Stack<TreeNode>) temp_stack.clone();
            temp_stack.clear();
            result.add(temp_list);
            if(label==0){
                label=1;
                continue;
            }
            if(label==1){
                label=0;
            }
        }

        System.out.println(result);
    }

}
相关推荐
yuren_xia1 小时前
Spring MVC中自定义日期类型格式转换器
java·spring·mvc
m0_504135301 小时前
代码随想录算法训练营第六十一天 | floyd算法
算法
GottdesKrieges2 小时前
OceanBase数据库磁盘空间管理
java·数据库·oceanbase
Themberfue3 小时前
Redis ⑦-set | Zset
java·开发语言·数据库·redis·sql·缓存
xin007hoyo5 小时前
算法笔记.染色法判断二分图
数据结构·笔记·算法
此木|西贝7 小时前
【设计模式】享元模式
java·设计模式·享元模式
এ᭄画画的北北7 小时前
力扣-234.回文链表
算法·leetcode·链表
李少兄7 小时前
解决Spring Boot多模块自动配置失效问题
java·spring boot·后端
bxlj_jcj8 小时前
JVM性能优化之年轻代参数设置
java·性能优化