Leetcode 103. 二叉树的锯齿形层序遍历(Medium)

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

示例 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

思路:这道题主要使用了两种方法,第一种就是正常层级遍历,但是要做一下手脚,使用两次层级遍历,第一次是先左子树再右子树的遍历方法,第二次是先右子树再左子树的遍历方法,然后根据奇偶,分别插入一半的数据

/**
 * 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 {
    List<List<Integer>> result = new ArrayList<>();
    int depth;
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        // 先分析一下题目 乍一看很难 其实不就是两种情况 一个是左 一个是右 先左后右
        // 即偶左奇右 当当前层数为奇数时先遍历左子树 再遍历右子树 
        // 当当前层数为偶数时先遍历右子树 再遍历左子树 
        depth = getMaxDepth(root);
        // 还是老样子 先获取最大深度
        for(int i = 0; i < depth; i++) result.add(new ArrayList<Integer>());

        fun1(0, root);
        fun2(0, root);
        return result;
    }
    public void fun1(int level, TreeNode root) {
        if (root == null) return;
        
        if (level % 2 != 0) result.get(level).add(root.val);
        level++;
        // 偶数
        if (root.right != null) {
            fun1(level, root.right);            
        }
        if (root.left != null) {
            fun1(level, root.left);
        }
    }
    public void fun2(int level, TreeNode root) {
        if (root == null) return;
        if (level % 2 == 0) result.get(level).add(root.val);
        level++;
        // 偶数
        if (root.left != null) {
            fun2(level, root.left);
        }

        if (root.right != null) {
            fun2(level, root.right);            
        }
    }

            
    public int getMaxDepth(TreeNode root) {
        
        if (root == null) {
            return 0;
        }

        return Math.max(getMaxDepth(root.left)+1, getMaxDepth(root.right)+1);
    
    }
}

第二种方法,类似于广度优先遍历的思想,用一个list来存储节点,然后根据奇偶性来遍历存储,还有根据奇偶性来调用左右子树存储子节点

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    int depth;
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        
        depth = getMaxDepth(root);
        // 还是老样子 先获取最大深度
        for(int i = 0; i < depth; i++) result.add(new ArrayList<Integer>());

        // 先创建一个list
        List<TreeNode> list = new LinkedList<>();
        if (root == null) return result;

        list.add(root);
        int level = 0;
        while (list.size() != 0) {
            // 遍历输出
            for (TreeNode node : list) {
                result.get(level).add(node.val);
            }
            // 一个一个清 然后把子节点加进去
            int n = list.size();
            for(int i = n-1;i >= 0;i--){
                TreeNode node = list.get(i);
                list.remove(i);
                if (level % 2 == 0) {
                    if(node.right != null) list.add(node.right);
                    if(node.left != null)  list.add(node.left);
                } else {
                    if(node.left != null)  list.add(node.left);
                    if(node.right != null) list.add(node.right);
                }
            }
            
            level++;
        }
        return result;
    }

            
    public int getMaxDepth(TreeNode root) {
        
        if (root == null) {
            return 0;
        }

        return Math.max(getMaxDepth(root.left)+1, getMaxDepth(root.right)+1);
    
    }

}

解释一下,主要代码分为两部分,一个是正向遍历储存,一个是反向遍历添加子节点,因为是一奇一偶的,顺序必须反,类似于蛇形遍历,第一次的时候是从左到右,然后最右边的右子树开始到最左边,在从最左边的左子树开始,形成蛇形遍历。

或者正向遍历添加子节点,无论何时都是正向,遍历储存时,根据奇偶性决定正反向

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值