binary-tree-postorder-traversal——二叉树后续遍历
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree{1,#,2,3},
1
\
2
/
3
return[3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
递归方法
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
if(root==NULL)
return v;
TreeNode *left=root->left,*right=root->right;
postorderTraversal(left);
postorderTraversal(right);
v.push_back(root->val);
return v;
} vector<int> v;
};
非递归
处理并出栈时判断,该节点是否有子节点或其子节点是否被访问过。并记录上次处理的节点。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> v;
if(root==NULL)
return v;
stack<TreeNode *> tree;
tree.push(root);
TreeNode *pre=NULL;
while(!tree.empty()){
TreeNode *p = tree.top();
if((p->left==NULL&&p->right==NULL)||(pre!=NULL&&(p->left==pre||p->right==pre))){
v.push_back(p->val);
pre=p;
tree.pop();
}else{
if(p->right!=NULL)
tree.push(p->right);
if(p->left!=NULL)
tree.push(p->left);
}
}
return v;
}
};
binary-tree-postorder-traversal——二叉树后续遍历的更多相关文章
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- [LeetCode] 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 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 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 (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
随机推荐
- Python字符串操作详解
菜鸟学Python第五天 流程控制 for循环 while循环 VS for循环: while循环:称之为条件循环,循环的次数取决于条件何时为false for循环:称之为迭代器循环,循环的次数取决于 ...
- MFC中的CListControl控件
一直想要这种效果,无奈刚开始用了cListbox控件,不知道怎么生成背景的边框,找了好久资料,突然发现好像控件用错了. 用CListControl控件实现图中效果,超级开心~ 实现过程: 添加一个Li ...
- BRVAH(让RecyclerView变得更高效)(1)
本文来自网易云社区 作者:吴思博 对于RecyclerView, 我们重复编写着那一个又一个的列表界面,有的要分组,有的要添加广告头部.有的要不同类型item排列.等等需求,主要代码有大部分是重复的, ...
- 在xcode上把你的app多语言国际化(NSLocalizedString)
1.到project->info->localizations 下面的加号,添加你需要的语言 千万不要删除 base 否虽然我不知道有什么用,我是删了整个storyboard没了,很 ...
- hdu2022
#include <stdio.h> #include <math.h> #define here puts("go,go,go!\n") int main ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- SoapUI offen used scripts
[ Groovy Step ] //get properties from testCase, testSuite and project in groovy step def testCasePro ...
- 【Luogu】P2258子矩阵(状态压缩,DP)
233今天蒟蒻我连文化课都没听光想着这个了 然后我调了一下午终于过了!!! 一看数据范围似乎是状压,然而216等于65536.开一个65536*65536的二维数组似乎不太现实. 所以Rqy在四月还是 ...
- HackerRank# Red John is Back
原题地址 简单动归+素数判定,没用筛法也能过 代码: #include <cmath> #include <cstdio> #include <vector> #i ...
- Java:Session详解
以下情况,Session结束生命周期,Servlet容器将Session所占资源释放:1.客户端关闭浏览器2.Session过期3.服务器端调用了HttpSession的invalidate()方法. ...