二叉树的遍历总结
- 144.二叉树的前序遍历(opens new window)
- 145.二叉树的后序遍历(opens new window)
- 94.二叉树的中序遍历
二叉数的先中后序统一遍历法
public static void preOrder(BiTree root){BiTree p = root;LinkedList<BiTree> stack = new LinkedList<>();while(p != null || !stack.isEmpty()){if(p != null){System.out.print(p.val +"\t");stack.push(p);p = p.left;}else {p = stack.pop();p = p.right;}}}
public static void preOrder00(BiTree root){LinkedList<BiTree>stack = new LinkedList<>();stack.push(root);while(!stack.isEmpty()){BiTree node = stack.peek();if(node != null){stack.pop();if(node.right != null){stack.push(node.right);}if(node.left !=null){stack.push(node.left);}stack.push(node);stack.push(null);}else {stack.pop();BiTree p = stack.peek();stack.pop();System.out.print(p.val + "\t");}}System.out.println();}
public static void preOrder03(BiTree root){if(root == null){return;}LinkedList<BiTree> stack = new LinkedList<>();stack.push(root);Map<BiTree,Boolean> visited = new HashMap<>();while(!stack.isEmpty()){BiTree node = stack.peek();stack.pop();if(visited.getOrDefault(node,false)){System.out.print(node.val +"\t");continue;}if(node.right != null){stack.push(node.right);visited.put(node.right, false);}if(node.left != null){stack.push(node.left);visited.put(node.left,false);}stack.push(node);visited.put(node,true);}System.out.println("");}
public static void inOrder(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();BiTree p = root;while( p != null || !stack.isEmpty()){if(p != null){stack.push(p);p = p.left;}else {p = stack.pop();System.out.print(p.val + "\t");p = p.right;}}}
public static void inOrder00(BiTree root){LinkedList<BiTree> stack = new LinkedList<BiTree>();stack.push(root);while(!stack.isEmpty()){BiTree node = stack.peek();if(node != null){stack.pop();if(node.right != null){stack.push(node.right);}stack.push(node);stack.push(null);if(node.left != null){stack.push(node.left);}}else {stack.pop();node = stack.peek();stack.pop();System.out.print(node.val + "\t");}}System.out.println();}
public static void inOrder03(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();stack.push(root);Map<BiTree,Boolean> isVisited = new HashMap<BiTree,Boolean>();while(!stack.isEmpty()){BiTree node = stack.peek();stack.pop();if(isVisited.getOrDefault(node, false)){System.out.print(node.val +"\t");continue;}if(node.right != null){stack.push(node.right);isVisited.put(node.right, false);}stack.push(node);isVisited.put(node, false);if(node.left != null){stack.push(node.left);isVisited.put(node.left, false);}isVisited.put(node, true);}}
public static void postOrder00(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();stack.push(root);while(!stack.isEmpty()){BiTree p = stack.peek();if(p != null){stack.pop();stack.push(p);stack.push(null);if(p.right != null){stack.push(p.right);}if(p.left != null) {stack.push(p.left);}}else {stack.pop();p = stack.peek();System.out.print(p.val +"\t");stack.pop();}}System.out.println();}
public static void postOrder03(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();BiTree p = root;stack.push(p);Map<BiTree,Boolean> isVisited = new HashMap<>();while(!stack.isEmpty()){BiTree node = stack.peek();stack.pop();if(isVisited.getOrDefault(node,false)){System.out.print(node.val+"\t");continue;}stack.push(node);isVisited.put(node, true);if(node.right != null){stack.push(node.right);isVisited.put(node.right, false);}if(node.left != null){stack.push(node.left);isVisited.put(node.left, false);}}}
public static void postOrder(BiTree root){LinkedList<BiTree> stack = new LinkedList<BiTree>();BiTree p = root;BiTree r = null;while(p != null || !stack.isEmpty()){if(p != null){stack.push(p);p = p.left;}else {p = stack.peek();if (p.right != null && p.right != r) {p = p.right;} else {p = stack.pop();System.out.print(p.val + "\t");r = p;p = null;}}}}
107.二叉树的层次遍历 II
力扣题目链接(opens new window)
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> results = new ArrayList<List<Integer>>();LinkedList<TreeNode> queue = new LinkedList<TreeNode>();if(root == null){return results;}queue.offer(root);while(!queue.isEmpty()){int queueSize = queue.size();List<Integer> result = new ArrayList<Integer>();for(int i = 1; i <= queueSize; i++){TreeNode node = queue.poll();result.add(node.val);if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}if(i == queueSize){results.addFirst(result);}}}return results;}
199.二叉树的右视图
力扣题目链接(opens new window)
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
public List<Integer> rightSideView(TreeNode root) {List<Integer> results= new ArrayList<Integer>();if(root == null){return results;}Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.offer(root);while(!queue.isEmpty()){int queueSize = queue.size();for(int i = 1; i <= queueSize; i++){TreeNode node = queue.poll();if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}if(i == queueSize){results.add(node.val);}}}return results;}
429.N叉树的层序遍历
力扣题目链接(opens new window)
给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
例如,给定一个 3叉树 :
返回其层序遍历:
[ [1], [3,2,4], [5,6] ]
public List<List<Integer>> levelOrder(Node root) {LinkedList<Node> queue = new LinkedList<Node>();queue.offer(root);List<List<Integer>> results = new ArrayList<List<Integer>>();if(root == null){return results;}while(!queue.isEmpty()){List<Integer> result = new ArrayList<Integer>();int levelSize = queue.size();for(int i = 1; i <= levelSize; i++){Node node = queue.poll();result.add(node.val);if(node.children != null){for(int j = 0; j < node.children.size();j++){queue.offer(node.children.get(j));}}}results.add(result);}return results;}
相关文章:

二叉树的遍历总结
144.二叉树的前序遍历(opens new window)145.二叉树的后序遍历(opens new window)94.二叉树的中序遍历 二叉数的先中后序统一遍历法 public static void preOrder(BiTree root){BiTree p root;LinkedList<BiTree> stack new LinkedList<>();while(p ! null ||…...

win32相关(远程线程和远程线程注入)
远程线程和远程线程注入 CreateRemoteThread函数 作用:创建在另一个进程的虚拟地址空间中运行的线程 HANDLE CreateRemoteThread([in] HANDLE hProcess, // 需要在哪个进程中创建线程[in] LPSECURITY_ATTRIBUTES lpThreadAttributes, // 安全…...
【Go语言基础【5】】Go module概述:项目与依赖管理
文章目录 一、Go Module 概述二、Go Module 核心特性1. 项目结构2. 依赖查找机制 三、如何启用 Go Module四、创建 Go Module 项目五、Go Module 关键命令 一、Go Module 概述 Go Module 是 Go 1.11 版本(2018 年 8 月)引入的依赖管理系统,用…...

[Spring]-AOP
AOP场景 AOP: Aspect Oriented Programming (面向切面编程) OOP: Object Oriented Programming (面向对象编程) 场景设计 设计: 编写一个计算器接口和实现类,提供加减乘除四则运算 需求: 在加减乘除运算的时候需要记录操作日志(运算前参数、运算后结果)实现方案:…...

agent 开发
什么是 agent? Agent智能体(又称AI Agent)是一种具备自主感知、决策与行动能力的智能系统,其核心在于模仿人类的认知过程来处理复杂任务。以下是其关键特性和发展现状的综合分析: 一、核心定义与特征 ### 自主决策…...
多系统一键打包docker compose下所有镜像并且使用
本方法适合在已经pull好docker镜像正常使用的机器 将环境迁移到无网络 或者网络不好的机器使用 linux 用法 cd 到 docker-compose.yml 所在目录 ./save_compose_images.sh #!/bin/bash # 拉取镜像并保存为 .tar 文件 docker save $(docker-compose images | awk {print…...

Golang——5、函数详解、time包及日期函数
函数详解、time包及日期函数 1、函数1.1、函数定义1.2、函数参数1.3、函数返回值1.4、函数类型与变量1.5、函数作参数和返回值1.6、匿名函数、函数递归和闭包1.7、defer语句1.8、panic和recover 2、time包以及日期函数2.1、time.Now()获取当前时间2.2、Format方法格式化输出日期…...
【HarmonyOS 5】出行导航开发实践介绍以及详细案例
以下是 HarmonyOS 5 出行导航的核心能力详解(无代码版),聚焦智能交互、多端协同与场景化创新: 一、交互革新:从被动响应到主动服务 意图驱动导航 自然语义理解:用户通过语音指令(如…...

深度学习环境配置指南:基于Anaconda与PyCharm的全流程操作
一、环境搭建前的准备 1. 查看基础环境位置 conda env list 操作说明:通过该命令确认Anaconda默认环境(base)所在磁盘路径(如D盘),后续操作需跳转至该磁盘根目录。 二、创建与激活独立虚拟环境 1. 创…...
03 Deep learning神经网络的编程基础 代价函数(Cost function)--吴恩达
深度学习中的损失函数(Cost Function)用于量化模型预测与真实数据的差距,是优化神经网络的核心指标。以下是常见类型及数学表达: 核心原理 逻辑回归通过sigmoid函数将线性预测结果转换为概率: y ^ ( i ) \hat{y}^{(i)}...

打卡day46
知识点回顾: 不同CNN层的特征图:不同通道的特征图什么是注意力:注意力家族,类似于动物园,都是不同的模块,好不好试了才知道。通道注意力:模型的定义和插入的位置通道注意力后的特征图和热力图 内…...

在SpringBoot中使用AWS SDK实现邮箱验证码服务
1.依赖导入(maven) <dependency><groupId>software.amazon.awssdk</groupId><artifactId>ses</artifactId><version>2.31.46</version></dependency> 2.申请两个key 发件人邮箱需要验证: …...
AndroidR车机TextToSpeech音频焦点异常问题分析
一、引言 文章《Android车机之TextToSpeech》介绍了TextToSpeech的使用,当前较多座舱系统语音服务都接入了原生TextToSpeech接口调用。 我司自研语音TTS服务,也接入了此TTS接口调用,对外提供TextToSpeech能力,播报时由客户端Client自行管理音频焦点,播报前申请音频焦点,…...
ArcGIS Maps SDK for JavaScript:使用图层过滤器只显示FeatureLayer的部分要素
文章目录 引言1 需求场景分析2精确过滤实现方案2.1 基础过滤语法2.2 动态过滤实现 3 模糊查询进阶技巧3.1 LIKE操作符使用3.2 特殊字段处理 4. 性能优化与注意事项4.1 服务端vs客户端过滤4.2 最佳实践建议 5 常见问题解答 引言 在地图应用开发中,图层过滤是常见的需…...

深入理解二叉搜索树:原理到实践
1.二叉搜索树的概念 ⼆叉搜索树⼜称⼆叉排序树,它或者是⼀棵空树,或者是具有以下性质的⼆叉树 若它的左树不为空,则左子树上所有节点的值都小于或等于根节点的值。若它的右树不为空,则右子树上所有节点的值都大于或等于根节点的…...

测试W5500的第11步_使用ARP解析IP地址对应的MAC地址
本文介绍了基于W5500芯片的ARP协议实现方法,详细阐述了ARP请求与回复的工作机制。ARP协议通过广播请求和单播回复实现IP地址与MAC地址的映射,确保局域网设备间的可靠通信。文章提供了完整的STM32F10x开发环境下的代码实现,包括网络初始化、SP…...

终极数据结构详解:从理论到实践
终极数据结构详解:从理论到实践 我将从 底层原理、时间复杂度、空间优化、实际应用 和 代码实现 五个维度,彻底解析数据结构。内容涵盖: 线性结构(数组、链表、栈、队列)非线性结构(树、图)高…...
STM32实战: CAN总线数据记录仪设计方案
以下是基于STM32的CAN总线数据记录仪/转发器的设计与实现方案,结合了核心功能和进阶需求: 系统架构 graph TBA[CAN总线] -->|CAN_H/CAN_L| B(STM32 bxCAN)B --> C[数据处理核心]C --> D[SD卡存储<br>FATFS文件系统]C --> E[串口输出…...

【k8s】k8s集群搭建
k8s集群搭建 一、环境准备1.1 集群类型1.2 安装方式1.3 主机规划1.4 环境配置1.4.1 说明1.4.2 初始化1.4.3 关闭防火墙和禁止防火墙开机启动1.4.4 设置主机名1.4.5 主机名解析1.4.6 时间同步1.4.7 关闭selinux1.4.8 关闭swap分区1.4.9 将桥接的IPv4流量传递到iptables的链1.4.1…...

60天python训练计划----day45
DAY 45 Tensorboard使用介绍 知识点回顾: tensorboard的发展历史和原理tensorboard的常见操作tensorboard在cifar上的实战:MLP和CNN模型 之前的内容中,我们在神经网络训练中,为了帮助自己理解,借用了很多的组件&#x…...
Python训练营打卡Day46(2025.6.6)
知识点回顾: 不同CNN层的特征图:不同通道的特征图什么是注意力:注意力家族,类似于动物园,都是不同的模块,好不好试了才知道。通道注意力:模型的定义和插入的位置通道注意力后的特征图和热力图 i…...

C# Wkhtmltopdf HTML转PDF碰到的问题
最近碰到一个Html转PDF的需求,看了一下基本上都是需要依赖Wkhtmltopdf,需要在Windows或者linux安装这个可以后使用。找了一下选择了HtmlToPDFCore,这个库是对Wkhtmltopdf.NetCore简单二次封装,这个库的好处就是通过NuGet安装HtmlT…...

Vue3 (数组push数据报错) 解决Cannot read property ‘push‘ of null报错问题
解决Cannot read property ‘push‘ of null报错问题 错误写法 定义变量 <script setup>const workList ref([{name:,value:}])</script>正确定义变量 <script setup>const workList ref([]) </script>解决咯~...
Lifecycle 核心原理面试回答
1. 核心目标与设计思想 解耦生命周期管理: 将 Activity/Fragment 的生命周期回调逻辑从视图控制器中剥离,让业务组件(如 Presenter, Repository 封装)能独立感知生命周期。 状态驱动: 将离散的生命周期事件 (ON_CREAT…...
PHP:Web 开发的强大基石与未来展望
在当今数字化时代,Web 开发技术日新月异,各种编程语言和框架层出不穷。然而,PHP 作为一种历史悠久且广泛应用的服务器端脚本语言,依然在 Web 开发领域占据着重要地位。 PHP 的历史与现状 PHP(Hypertext Preprocessor…...

html文字红色粗体,闪烁渐变动画效果,中英文切换版本
1. 代码 <!DOCTYPE html> <html lang"zh"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>红色粗体闪烁文字表格 - 中英文切换</t…...
六、【ESP32开发全栈指南:深入解析ESP32 IDF中的WiFi AP模式开发】
1. 引言:AP模式的核心价值 ESP32的AP(Access Point)模式使设备成为独立无线热点,适用于: 设备配网(SmartConfig)无路由器场景的本地组网数据直采终端(传感器集中器)临时…...

基于Django开发的运动商城系统项目
运动商城系统项目描述 运动商城系统是一个基于现代Web技术构建的电子商务平台,专注于运动类商品的在线销售与管理。该系统采用前后端分离架构,前端使用Vue.js实现动态交互界面,后端基于Django框架提供RESTful API支持,数据库采用…...
Python训练营打卡Day42
知识点回顾 回调函数lambda函数hook函数的模块钩子和张量钩子Grad-CAM的示例 1. 回调函数(Callback Function) 回调函数是作为参数传递给另一个函数的函数,目的是在某个事件发生后执行。 def fetch_data(callback):# 模拟数据获取data {&quo…...
https相比http的区别
https相比http的区别 https相比http的区别在于:https使用了SSL/TLS加密协议,确保数据传输的安全性和完整性,通信时需要证书验证。 https相比于http的区别主要在于安全性。https使用SSL/TLS加密传输数据,确保数据在客户端和服务器之间的通信…...