《剑指offer》之从上到下打印二叉树Ⅰ、Ⅱ、Ⅲ

简介: 《剑指offer》之从上到下打印二叉树Ⅰ、Ⅱ、Ⅲ

《剑指offer》之从上到下打印二叉树Ⅰ、Ⅱ、Ⅲ

1 从上到下打印二叉树Ⅰ

1.1 题目

https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/

1.2 思路

网络异常,图片无法展示
|


1.3 题解

二叉树类(下同):

/**
 * @desc: 二叉树类
 * @author: YanMingXin
 * @create: 2021/9/13-15:52
 **/
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) {
        val = x;
    }
}
复制代码

算法代码:

/**
 * @desc: 剑指 Offer 32 - I. 从上到下打印二叉树
 * @author: YanMingXin
 * @create: 2021/9/13-14:28
 **/
public class Test01 {
    /**
     * 声明队列
     */
    private Queue<TreeNode> queue = new LinkedList<>();
    /**
     * 声明有序列表
     */
    private ArrayList<Integer> list = new ArrayList<>();
    /**
     * BFS解决
     *
     * @param root
     * @return
     */
    public int[] levelOrder(TreeNode root) {
        //判断临界值
        if (root == null) {
            return new int[0];
        }
        //将root放在队首
        queue.add(root);
        //进行循环
        while (!queue.isEmpty()) {
            //弹出队首元素放在List中
            TreeNode node = queue.poll();
            list.add(node.val);
            //广度优先搜索
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
        //将List换成数组
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}
复制代码

2 从上到下打印二叉树Ⅱ

2.1 题目

https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/

2.2 思路

网络异常,图片无法展示
|


2.3 题解

/**
 * @desc: 剑指 Offer 32 - II. 从上到下打印二叉树 II
 * @author: YanMingXin
 * @create: 2021/9/13-14:52
 **/
public class Test02 {
    /**
     * 声明嵌套链表
     */
    private ArrayList<List<Integer>> list = new ArrayList<>();
    /**
     * 声明队列
     */
    private Queue<TreeNode> queue = new LinkedList<>();
    /**
     * BFS
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        //临界条件
        if (root != null) {
            queue.add(root);
        }
        while (!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            //将队列中的元素全部弹出
            for (int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                //list添加队列中弹出的元素
                tmp.add(node.val);
                //向队列添加新的元素,留作下次循环使用
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            list.add(tmp);
        }
        return list;
    }
}
复制代码

3 从上到下打印二叉树Ⅲ

3.1 题目

https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/

3.2 思路

何为双端队列?

双端队列顾名思义就是既可以在队首插入也可以在队尾弹出的队列。

因此如果想要达到题目的要求,就可以先判断是奇数还是偶数,并进行队首插入或队尾插入。

网络异常,图片无法展示
|


3.3 题解

/**
 * @desc: 剑指 Offer 32 - III. 从上到下打印二叉树 III
 * @author: YanMingXin
 * @create: 2021/9/13-15:08
 **/
public class Test03 {
    /**
     * 声明双端队列
     */
    private Queue<TreeNode> queue = new LinkedList<>();
    /**
     * 嵌套List
     */
    private List<List<Integer>> res = new ArrayList<>();
    /**
     * BFS+双端队列
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root != null) {
            queue.add(root);
        }
        while (!queue.isEmpty()) {
            LinkedList<Integer> tmp = new LinkedList<>();
            for (int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                if (res.size() % 2 == 0) {
                    // 偶数层 -> 队列头部
                    tmp.addLast(node.val);
                } else {
                    // 奇数层 -> 队列尾部
                    tmp.addFirst(node.val);
                }
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            res.add(tmp);
        }
        return res;
    }
}


相关文章
|
7天前
|
人工智能 运维 安全
|
5天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
607 21
|
12天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
970 110
|
6天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。