当前位置:网站首页>416-二叉树(前中后序遍历—迭代法)
416-二叉树(前中后序遍历—迭代法)
2022-06-24 09:33:00 【liufeng2023】
1、前序遍历(迭代法)
为什么要先加入 右孩子,再加入左孩子呢?
因为这样出栈的时候才是中左右的顺序。
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
result.push_back(node->val);
if (node->right) st.push(node->right); // 右(空节点不入栈)
if (node->left) st.push(node->left); // 左(空节点不入栈)
}
return result;
}
};
2、中序遍历(迭代法)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
// 指针来访问节点,访问到最底层
st.push(cur); // 将访问的节点放进栈
cur = cur->left; // 左
} else {
cur = st.top(); // 从栈里弹出的数据,就是要处理的数据(放进result数组里的数据)
st.pop();
result.push_back(cur->val); // 中
cur = cur->right; // 右
}
}
return result;
}
};
3、后序遍历(迭代法)
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
result.push_back(node->val);
if (node->left) st.push(node->left); // 相对于前序遍历,这更改一下入栈顺序 (空节点不入栈)
if (node->right) st.push(node->right); // 空节点不入栈
}
reverse(result.begin(), result.end()); // 将结果反转之后就是左右中的顺序了
return result;
}
};
边栏推荐
- 二叉樹第一部分
- LeetCode: 240. 搜索二维矩阵 II
- e的lnx为什么等于x
- 居家办公如何管理数据中心网络基础设施?
- Reasons for the failure of digital transformation and the way to success
- Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
- Grpc local test joint debugging tool bloomrpc
- Literature Research Report
- 二叉树第一部分
- LeetCode: 377. 组合总和 Ⅳ
猜你喜欢
Literature Research Report
Conseils étonnants pour promouvoir les ventes d'entreprise avec le chat en direct
Arbre binaire partie 1
latex公式及表格识别
PTA猴子选大王(约瑟夫环问题)
indexedDB本地存储,首页优化
20、 Processor scheduling (RR time slice rotation, mlfq multi-level feedback queue, CFS fully fair scheduler, priority reversal; multiprocessor scheduling)
深度学习论文阅读目标检测篇(七)中英对照版:YOLOv4《Optimal Speed and Accuracy of Object Detection》
R ellipse random point generation and drawing
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
随机推荐
如何提高网络基础设施排障效率,告别数据断档?
文献调研报告
Conseils étonnants pour promouvoir les ventes d'entreprise avec le chat en direct
Learning Tai Chi Maker - esp8226 (XIII) OTA
Why is LNX of e equal to X
SQL-统计连续N天登陆的用户
队列Queue
[GDB debugging tool] | how to debug under multithreading, multiprocessing and running programs
ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?
带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
LeetCode: 377. 组合总和 Ⅳ
Operator details
Endgame P.O.O
[Eureka source code analysis]
Oracle数据库监听文件配置
买的长期理财产品,可以转短吗?
How to locate lock waiting in Dameng database
How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
Symbol.iterator 迭代器
Oracle的tnsnames.ora文件配置