当前位置:网站首页>LeetCode94. 二叉树的中序遍历
LeetCode94. 二叉树的中序遍历
2022-07-28 11:49:00 【.DoubleBean.】
题目地址(94. 二叉树的中序遍历)
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
题目描述
给定一个二叉树的根节点 root ,返回它的 中序 遍历。
示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:
输入:root = [1,2]
输出:[2,1]
示例 5:
输入:root = [1,null,2]
输出:[1,2]
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
提示
中序遍历的迭代法有很多小细节,结合着树的知识点汇总这一篇里面的代码来理解
代码
- 语言支持:C++
C++ Code:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
TreeNode* node = NULL;
if(root){
// 为了应付 [] 这种情况,即root为NULL
st.push(root);
node = root;
}
while(!st.empty()){
while(node){
// 不能是node->left != NULL,否则会陷入死循环
node = node->left;
st.push(node);
}
if(!node){
node = st.top();
st.pop();
if(node){
// 判断node是否为NULL
result.push_back(node->val);
node = node->right;
st.push(node); // 不管node->right是否为NULL,直接压入栈中
}
}
}
return result;
}
};
Morris算法(莫里斯算法)
大佬题解,下面图片以及解析来源于此
用递归和迭代的方式都使用了辅助的空间,而莫里斯遍历的优点是没有使用任何辅助空间。
缺点是改变了整个树的结构,强行把一棵二叉树改成一段链表结构。
我们将黄色区域部分挂到节点5的右子树上,接着再把2和5这两个节点挂到4节点的右边。
这样整棵树基本上就变改成了一个链表了,之后再不断往右遍历。
复杂度
- 时间复杂度:
找到每个前驱节点的复杂度是 O(n),因为 n 个节点的二叉树有 n-1 条边,每条边只可能使用 2 次(一次定位到节点,一次找到前驱节点),故时间复杂度为 O(n) - 空间复杂度:
O(1)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
TreeNode *predecessor = NULL;
while(root){
if(root->left){
/*如果左结点不为空,就将当前结点连带右子树全部挂到左结点的最右子树下面*/
predecessor = root->left;
// predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
while(predecessor->right)
predecessor = predecessor->right;
predecessor->right = root;
//将root指向root的left
TreeNode* temp = root;
root = root->left;
temp->left = NULL;
}
else{
//左子树为空,则打印这个结点,并向右遍历
result.push_back(root->val);
root = root->right;
}
}
return result;
}
};
边栏推荐
- 用C语言开发NES游戏(CC65)04、完整的背景
- How does musk lay off staff?
- HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
- Developing NES game (cc65) 07 and controller with C language
- Hc-05 Bluetooth module debugging slave mode and master mode experience
- The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
- 揭秘界面控件DevExpress WinForms为何弃用受关注的MaskBox属性
- Using Arduino to develop esp8266 to build a development environment
- 【萌新解题】爬楼梯
- AI制药的数据之困,分子建模能解吗?
猜你喜欢

How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?

04 pyechars 地理图表(示例代码+效果图)

Developing NES games with C language (cc65) 11. Metatiles

Come to tdengine Developer Conference and have an insight into the future trend of data technology development

界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论

First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released

Design a thread pool

微创电生理通过注册:年营收1.9亿 微创批量生产上市企业

VS1003调试例程

洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东
随机推荐
西门子对接Leuze BPS_304i 笔记
VS code更新后不在原来位置
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
Develop NES game (cc65) 07 and controller with C language (collision with spirit)
Developing NES games with C language (cc65) 08. Background collision
C# 结构使用
Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
Distributed session solution
Using Arduino to develop esp8266 to build a development environment
Kuzaobao: summary of Web3 encryption industry news on July 13
苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
C structure use
Under what circumstances can the company dismiss employees
公司在什么情况下可以开除员工
Multi Chain and multi currency wallet system development cross chain technology
Deployment之滚动更新策略。
用C语言开发NES游戏(CC65)03、VRAM缓冲区
【萌新解题】爬楼梯