LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values.
Example:
Input: [,null,,]
\
/
Output: [,,]
Follow up: Recursive solution is trivial, could you do it iteratively?
方法一:利用两个栈s1,s2来实现,先将头结点入栈s1,从s1弹出栈顶节点记为cur,压入s2中,分别将cur的左右孩子压入s1,当s1为空后,s2的弹出节点次序就是后序遍历的次序。(C++)
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s1,s2;
s1.push(root);
vector<int> res={};
if(!root)
return res;
TreeNode* cur;
while(!s1.empty()){
cur=s1.top();
s1.pop();
s2.push(cur);
if(cur->left)
s1.push(cur->left);
if(cur->right)
s1.push(cur->right);
}
while(!s2.empty()){
cur=s2.top();
s2.pop();
res.push_back(cur->val);
}
return res;
}
方法二:先将头结点压入栈,怎样判断是该结点是应该输入vector中还是应该处理他的孩子?
1.该节点左右孩子为空时,为叶子结点,则该次遍历是输入到vector中
2.上一次输入的结点为该节点右孩子时,说明该结点的子树处理完毕,这次遍历是输入vector中
3.如果上一次输入的结点为该节点的左孩子,且右孩子为空,则该结点处理完毕,这次遍历就是输入vector中
4.否则说明子树没有被访问,按右、左孩子入栈。(C++)
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
vector<int> res={};
if(!root)
return res;
s.push(root);
TreeNode* last=NULL;
TreeNode* top;
while(!s.empty()){
top=s.top();
if((top->left==NULL&&top->right==NULL)||(top->right==NULL&&last==top->left)||(last==top->right&&last!=NULL)){
res.push_back(top->val);
last=top;
s.pop();
}
else{
if(top->right)
s.push(top->right);
if(top->left)
s.push(top->left);
}
}
return res;
}
注意此时要加上这个判定条件,若不加,输出的为2,1,没有把3这个结点入栈
方法三:递归方法(C++)
void postOrder(TreeNode* root,vector<int> &res){
if(!root)
return;
postOrder(root->left,res);
postOrder(root->right,res);
res.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
postOrder(root,res);
return res;
}
LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++的更多相关文章
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 145 Binary Tree Postorder Traversal 二叉树的后序遍历
给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...
随机推荐
- streamreader 和 streamwriter 以及 string 与 memorystream 使用示例
经常用到,但老记不住,备忘一下 using (var ms = new MemoryStream()) { var sw = new StreamWriter(ms); sw.WriteLine(&q ...
- php设置cookie为httponly防止xss攻击
什么是XSS攻击? XSS攻击(Cross Site Scripting)中文名为跨站脚本攻击,XSS攻击时web中一种常见的漏洞.通过XSS漏洞可以伪造目标用户登录,从而获取登录后的账号操作. 网站 ...
- 廖雪峰Git入门教程
廖雪峰Git入门教程 2018-05-24 23:05:11 0 0 0 https://www.liaoxuefeng.com/wiki/00137395163059296 ...
- @cacheevict 清除多个key
@Caching(evict={@CacheEvict(“a1”),@CacheEvict(“a2”,allEntries=true)}): 参考:https://www.cnblogs.com/ha ...
- 记录一次MyEclipse打开jsp文件出现Error的解决办法
今天正在忙着写项目,在打开一个项目内的jsp文件时发现我亲爱的代码消失了. 最后从网上找到了解决办法,希望可以帮到有需要的人,也作为个人记录 第一步:找到安装路径MyEclipse\configura ...
- day10 while else continue break
a. while else b. continue break continue ,终止当前循环,开始下一次循环 break ...
- oc 语法基础
1:https://mp.weixin.qq.com/s/IqICB4aW2vZdU0382S9sTA <1-7高质量OC代码> 2:https://blog.csdn.net/qq_3 ...
- Win10安装Ubuntu子系统教程(附安装图形化界面)
一.启用“适用于Linux的Windows子系统” 通过Win10任务栏中的Cortana搜索框搜索打开“启用或关闭Windows功能”,向下滚动列表,即可看到“适用于Linux的Windows子系统 ...
- Oracle查看表空间
知道表空间名,显示该表空间包括的所有表. select * from all_tables where tablespace_name='表空间名' 知道表名,查看该表属于那个表空间 select t ...
- laravel Cache store [] is not defined
去这个网站学习一下也好 https://laravel-china.org/topics/2093/laravel-source-analysis-series-cache#0b2791 如果env ...