145. Binary Tree Postorder Traversal
来源:互联网 发布:nbiot网络架构 编辑:程序博客网 时间:2024/06/07 06:25
/**
* Definition for a binary tree node.
* 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> res;
if(root == NULL) return res;
stack<TreeNode *> stackOfT;
TreeNode *tem = root;
TreeNode *last = root;
stackOfT.push(tem);
while (!stackOfT.empty())
{
tem = stackOfT.top();
if( (tem->left == NULL && tem->right == NULL) || (tem->right == NULL && last == tem->left) || (last == tem->right) )
{
res.push_back(tem->val);
last = tem;
stackOfT.pop();
}
else
{
if(tem->right != NULL) stackOfT.push(tem->right);
if(tem->left != NULL) stackOfT.push(tem->left);
}
}
return res;
}
};
* Definition for a binary tree node.
* 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> res;
if(root == NULL) return res;
stack<TreeNode *> stackOfT;
TreeNode *tem = root;
TreeNode *last = root;
stackOfT.push(tem);
while (!stackOfT.empty())
{
tem = stackOfT.top();
if( (tem->left == NULL && tem->right == NULL) || (tem->right == NULL && last == tem->left) || (last == tem->right) )
{
res.push_back(tem->val);
last = tem;
stackOfT.pop();
}
else
{
if(tem->right != NULL) stackOfT.push(tem->right);
if(tem->left != NULL) stackOfT.push(tem->left);
}
}
return res;
}
};
阅读全文
0 0
- 145.Binary Tree Postorder Traversal
- 145.Binary Tree Postorder Traversal
- 145.Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- 145. Binary Tree Postorder Traversal
- Android基础控件——SeekBar_1
- 517. Super Washing Machines
- 编程之战第八章 约瑟夫环(下)
- spring boot 入门
- 410. Split Array Largest Sum
- 145. Binary Tree Postorder Traversal
- 编程之战第九章 字符串与凶手(上)
- HTML+CSS编写静态网站-33 创建Contact页面
- webservice面试题
- 2017.7.12开始写博客
- 不重复随机数列的生成算法
- bzoj1119 [POI2009]SLO 置换
- jzoj1279 解题 (dp)
- 每日英语阅读(八)