代码随想录刷题笔记-Day12

1. 二叉树的递归遍历

144. 二叉树的前序遍历icon-default.png?t=N7T8https://leetcode.cn/problems/binary-tree-preorder-traversal/94. 二叉树的中序遍历icon-default.png?t=N7T8https://leetcode.cn/problems/binary-tree-inorder-traversal/145. 二叉树的后续遍历icon-default.png?t=N7T8https://leetcode.cn/problems/binary-tree-postorder-traversal/二叉树的深度优先遍历,是一个递归式的算法,对于递归,需要注意的就是每次递归的返回值,以及每次递归需要的参数,以及中止条件和进入递归的判断逻辑。

代码

  • 前序
class Solution {
	public List<Integer> preorderTraversal(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		pre(root, list);
		return list;
	}

	private void pre(TreeNode root, List<Integer> result) {
		if (root == null)
			return;
		result.add(root.val);
		pre(root.left, result);
		pre(root.right, result);
	}
}
  • 中序
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        inorder(root, list);
        return list;
    }

    private void inorder(TreeNode root, List<Integer> result) {
        if (root == null)
            return;
        inorder(root.left, result);
        result.add(root.val);
        inorder(root.right, result);
    }
}
  •  后序
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        postorder(root, list);
        return list;
    }

    private void postorder(TreeNode root, List<Integer> result) {
        if (root == null)
            return;
        postorder(root.left, result);
        postorder(root.right, result);
        result.add(root.val);
    }
}

 2. 二叉树的迭代遍历

使用栈进行迭代遍历

  • 前序
class Solution {
	public List<Integer> preorderTraversal(TreeNode root) {
		List<Integer> result = new ArrayList<>();
		if (root == null)
			return result;
		Stack<TreeNode> stack = new Stack<>();

		stack.push(root);
		while (!stack.isEmpty()) {
			TreeNode node = stack.pop();
			result.add(node.val);
			if (node.right != null) {
				stack.push(node.right);
			}
			if (node.left != null) {
				stack.push(node.left);
			}
		}

		return result;
	}
}
  • 中序

 中序遍历需要注意:

        中序遍历是先一路找到最左的,然后输出,每次输出就是处理一个node,输出后就得找到右节点的最左的。 

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null)
            return result;
        Stack<TreeNode> stack = new Stack<>();

        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();
                result.add(cur.val);
                cur = cur.right;
            }
        }

        return result;
    }
}
  • 后序
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null)
            return result;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            result.add(node.val);
            if (node.left != null) {
                stack.push(node.left);
            }
            if (node.right != null) {
                stack.push(node.right);
            }
        }
        Collections.reverse(result);
        return result;
    }
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/352669.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

混淆矩阵、准确率、查准率、查全率、DSC、IoU、敏感度的计算

1.背景介绍 在训练的模型的时候&#xff0c;需要评价模型的好坏&#xff0c;就涉及到混淆矩阵、准确率、查准率、查全率、DSC、IoU、敏感度的计算。 2、混淆矩阵的概念 所谓的混淆矩阵如下表所示&#xff1a; TP:真正类&#xff0c;真的正例被预测为正例 FN:假负类&#xf…

09. Springboot集成sse服务端推流

目录 1、前言 2、什么是SSE 2.1、技术原理 2.2、SSE和WebSocket 2.2.1、SSE (Server-Sent Events) 2.2.2、WebSocket 2.2.3、选择 SSE 还是 WebSocket&#xff1f; 3、Springboot快速集成 3.1、添加依赖 3.2、创建SSE控制器 3.2.1、SSEmitter创建实例 3.2.2、SSEmi…

K8s-持久化(持久卷,卷申明,StorageClass,StatefulSet持久化)

POD 卷挂载 apiVersion: v1 kind: Pod metadata:name: random-number spec:containers:- image: alpinename: alpinecommand: ["/bin/sh","-c"]args: ["shuf -i 0-100 -n 1 >> /opt/number.out;"]volumeMounts:- mountPath: /optname: da…

Ubuntu findfont: Font family ‘SimHei‘ not found.

matplotlib中文乱码显示 当我们遇到这样奇怪的问题时, 结果往往很搞笑 尝试1不行 Stopping Jupyter Installing font-manager: sudo apt install font-manager Cleaning the matplotlib cache directory: rm ~/.cache/matplotlib -fr Restarting Jupyter. 尝试2 This work fo…

AI大模型开发架构设计(6)——AIGC时代,如何求职、转型与选择?

文章目录 AIGC时代&#xff0c;如何求职、转型与选择&#xff1f;1 新职场&#xff0c;普通人最值钱的能力是什么?2 新职场成长的3点建议第1点&#xff1a;目标感第2点&#xff1a;执行力第3点&#xff1a;高效生产力 3 新职场会产生哪些新岗位机会?如何借势?4 新职场普通人…

微信小程序(十七)自定义组件生命周期(根据状态栏自适配)

注释很详细&#xff0c;直接上代码 上一篇 新增内容&#xff1a; 1.获取手机状态栏的高度 2.验证attached可以修改数据 3.动态绑定样式数值 源码&#xff1a; myNav.js Component({lifetimes:{//相当于vue的created,因为无法更新数据被打入冷宫created(){},//相当于vue的mount…

【运行Python爬虫脚本示例】

主要内容&#xff1a;Python中的两个库的使用。 1、requests库&#xff1a;访问和获取网页内容&#xff0c; 2、beautifulsoup4库&#xff1a;解析网页内容。 一 python 爬取数据 1 使用requests库发送GET请求&#xff0c;并使用text属性获取网页内容。 然后可以对获取的网页…

基于python flask茶叶网站数据大屏设计与实现,可以做期末课程设计或者毕业设计

基于Python的茶叶网站数据大屏设计与实现是一个适合期末课程设计或毕业设计的项目。该项目旨在利用Python技术和数据可视化方法&#xff0c;设计和开发一个针对茶叶行业的数据大屏&#xff0c;用于展示和分析茶叶网站的相关数据。 项目背景 随着互联网的快速发展&#xff0c;越…

一张图区分Spring Task的3种模式

是的&#xff0c;只有一张图&#xff1a; fixedDelay 模式cron 模式fixedRate 模式

[HUBUCTF 2022 新生赛]ezPython

打开是一个pyc文件&#xff0c;我用的是pycdc进行反编译 # Source Generated with Decompyle # File: ezPython.pyc (Python 3.7)from Crypto.Util.number import * import base64 import base58 password open(password.txt, r).read() tmp bytes_to_long(password.encode(u…

基于JavaWeb开发的家具定制购买网站【附源码】

基于JavaWeb开发的家具定制购买网站【附源码】 &#x1f345; 作者主页 央顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; &#x1f345; 查看下方微信号获取联系方式 承接各种定制系统 &#…

GLog开源库使用

Glog地址&#xff1a;https://github.com/google/glog 官方文档&#xff1a;http://google-glog.googlecode.com/svn/trunk/doc/glog.html 1.利用CMake进行编译&#xff0c;生成VS解决方案 &#xff08;1&#xff09;在glog-master文件夹内新建一个build文件夹&#xff0c;用…

docker-compose Install influxdb1+influxdb2+telegraf

influxd2前言 influxd2 是 InfluxDB 2.x 版本的后台进程,是一个开源的时序数据库平台,用于存储、查询和可视化时间序列数据。它提供了一个强大的查询语言和 API,可以快速而轻松地处理大量的高性能时序数据。 telegraf 是一个开源的代理程序,它可以收集、处理和传输各种不…

在Ubuntu上安装pycuda记录

1. 安装CUDA Toolkit 11.8 从MZ小师妹的摸索过程来看&#xff0c;其他版本的会有bug&#xff0c;12.0的版本太高&#xff0c;11.5的太低&#xff08;感谢小师妹让我少走弯路&#xff09; 参考网址&#xff1a;CUDA Toolkit 11.8 Downloads | NVIDIA Developer 在命令行输入命…

调用阿里通义千问大语言模型API-小白新手教程-python

阿里大语言模型通义千问API使用新手教程 最近需要用到大模型&#xff0c;了解到目前国产大模型中&#xff0c;阿里的通义千问有比较详细的SDK文档可进行二次开发,目前通义千问的API文档其实是可以进行精简然后学习的,也就是说&#xff0c;是可以通过简单的API调用在自己网页或…

Vue<圆形旋转菜单栏效果>

效果图&#xff1a; 大家不一定非要制成菜单栏&#xff0c;可以看下人家的华丽效果&#x1f61d;&#xff0c;参考地址 https://travelshift.com/ 大佬写的效果可比我的强多了&#xff0c;但是无从下手&#xff0c;所以就自己琢磨怎么写了&#xff0c;只能说效果勉强差不多 可…

“steam教学理念”scratch+数学 ——时钟案例

一、时钟概念 它通常由一个圆形表盘组成&#xff0c;表盘上有12个数字&#xff0c;分别是1到12。这些数字代表了小时。在表盘上&#xff0c;还有三根指针&#xff0c;一根较短的指针叫做时针&#xff0c;另一根较长的指针叫做分针&#xff0c;而秒针通常为红色&#xff0c;且指…

LabVIEW电缆检修系统

在电力系统中&#xff0c;合理选择电缆检修策略是保障电网稳定运行的关键。现有的电缆检修策略往往忽视了电缆的技术和经济双重指标&#xff0c;导致检修效率低下和维护成本高昂。为此&#xff0c;开发了一种基于风险评估模型和全寿命周期成本&#xff08;LCC&#xff09;的电缆…

java金额数字转中文

java金额数字转中文 运行结果&#xff1a; 会进行金额的四舍五入。 工具类源代码&#xff1a; /*** 金额数字转为中文*/ public class NumberToCN {/*** 汉语中数字大写*/private static final String[] CN_UPPER_NUMBER {"零", "壹", "贰",…

Springboot+Netty搭建基于TCP协议的服务端

文章目录 概要pom依赖Netty的server服务端类Netty通道初始化I/O数据读写处理测试发送消息 并 接收服务端回复异步启动Netty运行截图 概要 Netty是业界最流行的nio框架之一&#xff0c;它具有功能强大、性能优异、可定制性和可扩展性的优点 Netty的优点&#xff1a; 1.API使用简…
最新文章