Binary Tree Iterative Traversal
Preorder
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
stack<TreeNode *> sta;
sta.push(root);
while (!sta.empty())
{
TreeNode* cur = sta.top();
sta.pop();
res.push_back(cur->val);
if (cur->right) sta.push(cur->right);
if (cur->left) sta.push(cur->left);
}
return res;
}
};
postorder
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
stack<TreeNode *> sta;
sta.push(root);
TreeNode *last = NULL;
while (!sta.empty())
{
TreeNode *cur = sta.top();
TreeNode *left = cur->left, *right = cur->right;
if (left && (!last || (last != left && last != right)))
sta.push(left);
else if (right && (!last || last != right))
sta.push(right);
else
{
res.push_back(cur->val);
last = cur;
sta.pop();
}
}
return res;
}
};
inorder
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> sta;
while() {
while(root) { sta.push(root); root = root->left; }
if(sta.empty()) break;
root = sta.top(); sta.pop();
res.push_back(root->val);
root = root->right;
}
return res;
}
};
Binary Tree Iterative Traversal的更多相关文章
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LintCode Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
随机推荐
- springboot生成表结构
https://blog.csdn.net/yalishadaa/article/details/59656332
- python学习总结----时间模块 and 虚拟环境(了解)
time - sleep:休眠指定的秒数(可以是小数) - time:获取时间戳 # 获取时间戳(从1970-01-01 00:00:00到此刻的秒数) t = time.time() print(t ...
- vim使用的一些积累
vi visual interfacevim vi improved vim模式:编辑模式(命令模式)输入模式末行模式 编辑模式下,zz保存并退出移动光标:(编辑模式)1.逐字符移动 h 左 l 右 ...
- 台州学院we are without brain 训练 后缀数组
sa[i]表示排名为 i 的后缀的第一个字符在原串中的位置 rank[i]表示按照从小到大排名 以i为下标开始的后缀的排名 height[i]表示排名为 i 和排名为 i+1的后缀的最长公共前缀的长 ...
- 软工实践 - 第十六次作业 Alpha 冲刺 (7/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10013959.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...
- web自动化测试:watir+minitest(四)
脚本连跑: rake是ruby中的一个构建工具,和make很像.允许用ruby来写rakefile. 我们使用rake以任务的方式来运行我们的脚本集. 新建Rakefile文件,写入如下内容: req ...
- zkCli.sh客户端其他命令简介
quit 退出 connect host:port 连接到其他ZooKeeper服务器 close 关闭连接 history 操作指令历史 redo 重复执行某个指令, 使用格式为 redo id, ...
- 【BZOJ1123】 [POI2008]BLO (tarjan)
tarjan判断割点...拿掉一个点之后,会被分成若干个联通块,用节点个数和统计一下他们相互不能到达的个数就好. ; maxm=; type edgetype=record toward,next:l ...
- 怎么用dos命令进入指定的文件夹
在正常开发中经常需要我们进入指定的文件夹下面的例子演示了进入这个文件夹D:\portal\liferay-portal-tomcat-5.5-4.4.0的dos命令 win+R---->输入cm ...
- bzoj4418 [Shoi2013]扇形面积并
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4418 [题解] 被题目名称吓死系列. 用一棵线段树维护当前有哪些半径. 那么将扇形差分,每段 ...