剑指 Offer 32 - I.II.III. 从上到下打印二叉树(层序遍历)

本文详细讲解了如何通过基础的层序遍历方法解决剑指Offer中的三个二叉树层次打印问题,包括常规层序、之字形顺序以及利用双栈技巧。通过实例演示和两种解法,帮助读者理解并掌握不同情况下的二叉树层次遍历算法。
摘要由CSDN通过智能技术生成

剑指 Offer 32 - I. 从上到下打印二叉树

问题描述

从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回:

[3,9,20,15,7]

提示:

    节点总数 <= 1000

解题思路

很容易想到的思路就是基础的层序遍历。只是一般的层序遍历迭代法是使用List<List<Integer>> 来存储的,但是本题要求的返回值为int [] 型,因此后面需要加一个格式转换。

Java解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root!=null) queue.offer(root);
        int sum = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            sum+=size;
            List<Integer> tmp = new ArrayList<>();
            for(int i=0;i<size;i++){
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
            }
            res.add(tmp);
        }
        int [] arr = new int [sum];
        int ind = 0;
        for(List<Integer> tmp:res){
            for(Integer i: tmp){
                arr[ind++]=i;
            }
        }
        return arr;
    }
}

剑指 Offer 32 -II. 从上到下打印二叉树

问题描述

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

提示:

    节点总数 <= 1000

解题思路

依然基于上面的层序遍历解题方法,输出结果是按层存储的。

Java解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root!=null) queue.offer(root);
        int sum = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            sum+=size;
            List<Integer> tmp = new ArrayList<>();
            for(int i=0;i<size;i++){
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
            }
            res.add(tmp);
        }
        
        return res;
    }
}

剑指 Offer 32 - III. 从上到下打印二叉树 III

问题描述

请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [20,9],
  [15,7]
]

提示:

    节点总数 <= 1000

解题思路

解法一(使用reverse方法):

很容易想到的思路就是在基础的层序遍历之上。

对层数做判断,假如是奇数,将这一层遍历的集合reverse(逆序)一下,再存入List<List<Integer>>中。

解法二(双栈法):

Java解法

解法一:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root!=null) queue.offer(root);
        int count=1;
        while(!queue.isEmpty()){
            int size = queue.size();
            count+=1;
            List<Integer> tmp = new ArrayList<>();
            for(int i=0;i<size;i++){
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left!=null) queue.offer(node.left);
                if(node.right!=null) queue.offer(node.right);
            }
            if((count&1)==1){
                Collections.reverse(tmp);
            }
            res.add(tmp);
        }
        return res;
    }
}

解法二:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {

        List<List<Integer>> ret = new ArrayList<>();
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        int depth = 0; // 记录行号
        if (root != null) {
            stack1.push(root);
        }
        // 当两个栈都为空时停止循环
        while (!stack1.isEmpty() || !stack2.isEmpty()) {
            List<Integer> res = new ArrayList<>();
            depth++;
            if ((depth &1)== 1) { // 奇数行
                for (int i = stack1.size(); i>0; i--) {
                    TreeNode node = stack1.pop();
                    res.add(node.val);
                    if (node.left != null) stack2.add(node.left);
                    if (node.right != null) stack2.add(node.right);
                }
            } else {
                for (int i = stack2.size(); i > 0; i--) {
                    TreeNode node = stack2.pop();
                    res.add(node.val);
                    if (node.right != null) stack1.push(node.right);
                    if (node.left != null) stack1.push(node.left);
                }
            }
            ret.add(res);
        }
        return ret;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值