C++实现LeetCode(144.二叉树的先序遍历)
作者:Grandyang 时间:2023-12-22 19:41:57
[LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input:
[1,null,2,3]
1
\
2
/
3
Output:
[1,2,3]
Follow up: Recursive solution is trivial, could you do it iteratively?
一般我们提到树的遍历,最常见的有先序遍历,中序遍历,后序遍历和层序遍历,它们用递归实现起来都非常的简单。而题目的要求是不能使用递归求解,于是只能考虑到用非递归的方法,这就要用到stack来辅助运算。由于先序遍历的顺序是"根-左-右", 算法为:
1. 把根节点 push 到栈中
2. 循环检测栈是否为空,若不空,则取出栈顶元素,保存其值,然后看其右子节点是否存在,若存在则 push 到栈中。再看其左子节点,若存在,则 push 到栈中。
参见代码如下:
解法一:
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
if (!root) return {};
vector<int> res;
stack<TreeNode*> s{{root}};
while (!s.empty()) {
TreeNode *t = s.top(); s.pop();
res.push_back(t->val);
if (t->right) s.push(t->right);
if (t->left) s.push(t->left);
}
return res;
}
};
下面这种写法使用了一个辅助结点p,这种写法其实可以看作是一个模版,对应的还有中序和后序的模版写法,形式很统一,方便于记忆。辅助结点p初始化为根结点,while 循环的条件是栈不为空或者辅助结点p不为空,在循环中首先判断如果辅助结点p存在,那么先将p加入栈中,然后将p的结点值加入结果 res 中,此时p指向其左子结点。否则如果p不存在的话,表明没有左子结点,取出栈顶结点,将p指向栈顶结点的右子结点,参见代码如下:
解法二:
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
TreeNode *p = root;
while (!st.empty() || p) {
if (p) {
st.push(p);
res.push_back(p->val);
p = p->left;
} else {
p = st.top(); st.pop();
p = p->right;
}
}
return res;
}
};
来源:https://www.cnblogs.com/grandyang/p/4146981.html
标签:C++,二叉树的先序遍历,LeetCode
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JMeter中的后端监听器的实现
2022-07-24 17:58:35
![](https://img.aspxhome.com/file/2023/8/84768_0s.jpg)
c# 实现语音合成
2021-06-16 00:17:47
![](https://img.aspxhome.com/file/2023/4/91314_0s.png)
spring boot 使用profile来分区配置的操作
2022-11-27 22:55:15
![](https://img.aspxhome.com/file/2023/5/64205_0s.png)
解决@CachePut设置的key值无法与@CacheValue的值匹配问题
2021-09-10 13:28:49
Java Bean与xml互相转换的方法分析
2021-08-12 13:34:00
java开发之MD5加密算法的实现
2022-05-13 23:44:35
java实现代码统计小程序
2022-03-08 23:15:24
C#使用Aspose.Cells创建和读取Excel文件
2022-11-24 17:47:23
![](https://img.aspxhome.com/file/2023/2/75592_0s.jpg)
SpringBoot上传临时文件被删除引起报错的解决
2022-05-28 23:46:24
![](https://img.aspxhome.com/file/2023/6/74886_0s.jpg)
java反射之方法反射的基本操作方法
2021-11-26 00:45:36
![](https://img.aspxhome.com/file/2023/7/128917_0s.jpg)
Spring框架学习之Cache抽象详解
2023-07-20 17:37:47
详解C++ STL模拟实现forward_list
2023-06-21 02:36:04
![](https://img.aspxhome.com/file/2023/0/60100_0s.jpg)
C#实现泛型动态循环数组队列的方法
2022-11-03 05:05:42
SpringBoot2.x过后static下的静态资源无法访问的问题
2023-07-07 00:21:09
![](https://img.aspxhome.com/file/2023/3/67623_0s.jpg)
一篇文章教会你用Unity制作网格地图生成组件
2023-09-05 04:44:36
![](https://img.aspxhome.com/file/2023/3/96313_0s.jpg)
javaweb图书商城设计之用户模块(1)
2023-10-30 09:22:57
![](https://img.aspxhome.com/file/2023/2/104002_0s.jpg)
AndroidStudio 3.6 中 R.layout 找不到对应的xml文件问题及解决方法
2023-05-19 08:22:02
![](https://img.aspxhome.com/file/2023/4/138564_0s.jpg)
Android DigitalClock组件用法实例
2022-11-12 18:18:28
![](https://img.aspxhome.com/file/2023/6/139646_0s.png)
游戏开发之随机概率的选择算法
2022-08-26 13:21:09
Android创建淡入淡出动画的详解
2022-12-28 00:12:12
![](https://img.aspxhome.com/file/2023/9/114339_0s.gif)