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 ...
随机推荐
- Ajaxpro使用的方法
1.下载Ajaxpro.2.dll 程序中引用 2.web.config配置 <?xml version="1.0" encoding="utf-8"?& ...
- 简述iproute家族命令
ifconfig 是用来查看.配置.启用或禁用网络接口的工具.可以用这个工具来临时配置网卡的IP地址.掩码.广播地址.网关等. 语法 ifconfig [interface] 参数 up 启动指定网络 ...
- C++中const的用法
1.const修饰普通变量和指针 (1).const修饰普通变量 其写法有2种:a.const type value; b.type const value; 这两种写法本质上是一样的.其含义是: ...
- Pagedown learning notes
Pagedown Links Google wiki page Download Markdown.Converter.js var converter = new Markdown.Converte ...
- 2D游戏与3D游戏的区别 原文:https://zhidao.baidu.com/question/588490865.html
2D和3D间有哪些不同点呢? 让我们来比较一下,共同找出它俩之间的不同点. 对玩家来说,2D技术和3D技术只是显示数据的方式而已,玩家都是通过二 维的平面显示器来观看它们.对制作者来说,二者的不同之处 ...
- js获取时间戳的三种方法
1.Date.Now() 2.new Date().getTime() 3.Date.parse(new Date()) 其中1和2是相同含义 chrome控制台键入:Date.now() ===ne ...
- Android studio 中添加依赖model时依赖所需的准备
例如向app中添加依赖core: core要做如下修改: 1.将core中build.gradle中 修改为 . 2.将core中的 applicationId 注释掉.
- ID 生成器 雪花算法
https://blog.csdn.net/wangming520liwei/article/details/80843248 ID 生成器 雪花算法 2018年06月28日 14:58:43 wan ...
- guava Lists.transform使用
作用:将一个List中的实体类转化为另一个List中的实体类. 稍微方便一点.例如:将List<Student>转化为List<StudentVo> Student: pack ...
- 微信小程序中的组件使用2
需求 上面两个页面是同一个小程序的不同页面,两个页面中都是用到了label,有相似的地方,但是也有不同之处,这个时候,如果我们想要将这些label做出组件,然后复用,有该怎么做呢? 基础组件 首 ...