当前位置:网站首页>刷题笔记(十三)--二叉树:前中后序遍历(复习)
刷题笔记(十三)--二叉树:前中后序遍历(复习)
2022-06-11 11:34:00 【梦想成为光头强!】
系列文章目录
刷题笔记(一)–数组类型:二分法
刷题笔记(二)–数组类型:双指针法
刷题笔记(三)–数组类型:滑动窗口
刷题笔记(四)–数组类型:模拟
刷题笔记(五)–链表类型:基础题目以及操作
刷题笔记(六)–哈希表:基础题目和思想
刷题笔记(七)–字符串:经典题目
刷题笔记(八)–双指针:两数之和以及延伸
刷题笔记(九)–字符串:KMP算法
刷题笔记(十)–栈和队列:基础题目
刷题笔记(十一)–栈和队列:Top-K问题
刷题笔记(十二)–复习:排序算法
题录
144. 二叉树的前序遍历
题目链接如下:
题目截屏如下:
解法如下:
递归写法
public class 前序遍历 {
public List<Integer> preorderTraversal(TreeNode root) {
//构造一个List用来返回
List<Integer> list = new ArrayList<>();
//然后开始前序遍历
preorderTraversal(list,root);
return list;
}
public void preorderTraversal(List<Integer> list,TreeNode root){
//如果为空就直接返回
if(root == null){
return;
}
//前序遍历:根节点 --> 左子树 --> 右子树
list.add(root.val);
preorderTraversal(list,root.left);
preorderTraversal(list,root.right);
}
}
迭代写法
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();//构造一个list用来接收root值
Stack<TreeNode> stack = new Stack<>();//构造一个栈用来前序遍历
//不为空就把root入栈
if(root != null){
stack.push(root);
}
//栈不为空就开始遍历
while(!stack.isEmpty()){
TreeNode node = stack.pop();
list.add(node.val);
//栈的特性:先进后出,所以右子树先入栈后出
if(node.right != null){
stack.push(node.right);
}
//左子树后入栈先出
if(node.left != null){
stack.push(node.left);
}
}
return list;
}
94. 二叉树的中序遍历
题目链接如下:
题目截图如下:

递归写法
递归写法和上面的差不多,就是调换了一下位置
public class 中序遍历_递归写法 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorderTraversal(list,root);
return list;
}
public void inorderTraversal(List<Integer> list,TreeNode root) {
if(root == null){
return;
}
inorderTraversal(list,root.left);
list.add(root.val);
inorderTraversal(list, root.right);
}
}
迭代写法
public class 中序遍历_迭代写法 {
public List<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
List<Integer> list = new ArrayList<>();
TreeNode cur = root;
//栈为空并且cur == null的时候就遍历完毕
while(!stack.isEmpty() || cur != null){
//首先就是往左走,一直往左走,走到没有地方走为止
if(cur != null){
stack.push(cur);
cur = cur.left;
}else{
//如果说左子树为null的时候,就把当前节点值加入,然后遍历右子树的左子树
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
145. 二叉树的后序遍历
题目链接如下:
题目截屏如下:

递归写法
public class 后序遍历_递归 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
postorderTraversal(list,root);
return list;
}
public void postorderTraversal(List<Integer> list,TreeNode root) {
if(root == null){
return;
}
postorderTraversal(list,root.left);
postorderTraversal(list,root.right);
list.add(root.val);
}
}
迭代写法–1
这里的话其实就是把前序遍历得到的list翻转一下
public class 后序遍历_迭代 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root != null) {
stack.push(root);
}
while(!stack.empty()){
TreeNode node = stack.pop();
list.add(node.val);
if(node.left != null){
stack.push(node.left);
}
if(node.right != null){
stack.push(node.right);
}
}
Collections.reverse(list);
return list;
}
}
迭代写法–2
public class 后续遍历_迭代2 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
// cur 用来遍历
TreeNode cur = root;
// prev 用来记录上次遍历的个结点
TreeNode prev = null;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.add(cur);
cur = cur.left;
} else {
cur = stack.pop();
if (cur.right != null && cur.right != prev) {
// 有右子树并且右子树没遍历,需放回根结点到栈中
stack.push(cur);
// 接下来遍历右子树
cur = cur.right;
} else {
// 没有右子树,或者右子树被遍历过,
res.add(cur.val);
// 更新前指针
prev = cur;
// 以该结点的子树都已遍历,下次需要从栈中取父亲结点向上遍历,所以 cur == null
cur = null;
}
}
}
return res;
}
}
边栏推荐
- 《公司理财师专业能力》笔记
- 中级web开发工程师,面试题+笔记+项目实战
- Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
- Node连接MySql数据库写模糊查询接口
- Use compiler option '--downleveliteration' to allow iteration of iterations
- Iframe value transfer
- Queuing theory model
- Collection of practical WordPress plug-ins (under update)
- 快速搭建ELK7.3
- nft数字藏品app系统搭建
猜你喜欢

再不刷题就晚了,最全的BAT大厂面试题整理

木瓜移动CFO刘凡 释放数字时代女性创新力量

Uncaught TypeError: Cannot set property ‘next‘ of undefined 报错解决

Template engine - thymeleaf

01_ Description object_ Class diagram
![Set the default receiving address [project mall]](/img/eb/2864b124b66d01849315a367948ed4.png)
Set the default receiving address [project mall]

Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr

Use of Chinese input method input event composition

浙大联合微软亚研院发布视频识别新方法,可对视频逐帧识别且无需,数据标记,或可用于手语翻译等

How to form a good habit? By perseverance? By determination? None of them!
随机推荐
Split data - horizontal split and vertical split
Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!
统计出现次数最多的前K个字符串
ELK - ElastAlert最大的坑
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)
Cap theory sounds very big, but it's actually very simple
WP Super Cache静态缓存插件简明使用教程
Where is it safer to open an account for soda ash futures? How is the deposit calculated?
17.4创建多个线程、数据共享问题分析与案例代码
推荐几款Gravatar头像缓存插件
Problems encountered in installing mysql8 under centos7.x couldn't open file /etc/pki/rpm-gpg/rpm-gpg-key-mysql-2022
17.4 creating multiple threads, data sharing problem analysis and case code
WordPress用户名修改插件:Username Changer
nft数字藏品app系统搭建
Intermediate web development engineer, interview questions + Notes + project practice
Dominating set, independent set, covering set
Etcd介绍
Liufan, CFO of papaya mobile, unleashes women's innovative power in the digital age
Let WordPress support registered users to upload custom avatars
Use compiler option '--downleveliteration' to allow iteration of iterations