LeetCode145: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?
解题思路:
后序遍历的非递归实现与前序遍历和中序遍历的非递归不同,对于根节点,必须当其右孩子都访问完毕后才能访问,所以当根节点出栈时,要根据标志位判断是否为第一次出栈,如果是,则将其标志位置为FALSE,然后再次入栈,先访问其右孩子,当某个节点出栈时且其标志位为false,说明该节点为第二次出栈,即表示其右孩子都已处理完毕,可以访问当前节点了
实现代码:
#include <iostream>
#include <vector>
#include <stack>
using namespace std; /**
Given a binary tree, return the postorder traversal of its nodes' values.
*/ struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; void addNode(TreeNode* &root, int val)
{
if(root == NULL)
{
TreeNode *node = new TreeNode(val);
root = node;
}
else if(root->val < val)
{
addNode(root->right, val);
}
else if(root->val > val)
{
addNode(root->left, val);
}
} void printTree(TreeNode *root)
{
if(root)
{
cout<<root->val<<" ";
printTree(root->left);
printTree(root->right);
}
}
struct Node
{
TreeNode *treeNode;
bool isFirst;
}; class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ivec;
if(root)
{
stack<Node*> istack;
TreeNode *p = root;
while(!istack.empty() || p)
{
while(p)
{
Node *tmpNode = new Node();
tmpNode->treeNode = p;
tmpNode->isFirst = true;
istack.push(tmpNode);
p = p->left;
}
if(!istack.empty())
{
Node *node = istack.top();
istack.pop();
if(node->isFirst)//第一次出栈,
{
node->isFirst = false;
istack.push(node);
p = node->treeNode->right;
}
else//表示其右孩子都已经访问了,可以访问当前节点了
{
ivec.push_back(node->treeNode->val);
}
} } }
return ivec; }
};
int main(void)
{
TreeNode *root = new TreeNode();
addNode(root, );
addNode(root, );
addNode(root, );
addNode(root, );
printTree(root);
cout<<endl; Solution solution;
vector<int> v = solution.postorderTraversal(root);
vector<int>::iterator iter;
for(iter = v.begin(); iter != v.end(); ++iter)
cout<<*iter<<" ";
cout<<endl; return ;
}
LeetCode145:Binary Tree Postorder Traversal的更多相关文章
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 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 (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- Ubuntu中解决机箱前置耳机没声音
Ubuntu中解决机箱前置耳机没声音 安装pavucontrol软件: sudo apt-get install pavucontrol 然后直接运行pavucontrol打开软件: 将输出设备设置为 ...
- numpy.unpackbits()
numpy.unpackbits numpy.unpackbits(myarray, axis=None) Unpacks elements of a uint8 array into a binar ...
- spine
spine 英 [spʌɪn] 美 [spaɪn] n.脊柱;[动,植] 棘,刺(如刺猬和海胆的刺);鱼鳍的刺;植物上的刺
- K组翻转链表 · Reverse Nodes in k-Group
[抄题]: 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. [思维问题]: [一句话思路]: // reverse head->n1 ...
- 记录java版本不兼容的坑,(kafka运行报错)
启动kafka报错 错误原因是: 由较高版本的jdk编译的java class文件 试图在较低版本的jvm上运行的报错 解决办法是: 查看java版本 C:\Users\Administrator&g ...
- TPshop学习
笔记大纲: tpshop目录结构 功能模块 函数库 重要配置 助手函数 插件 模板 1.TPshop目录结构 目录结构(来自官方视频截图) 看这个图,目录结构一目了然.下面要讲的内容也是根据这个图展开 ...
- webstorm使用教程
Webstorm 超实用配置教程 原文来自:http://www.jianshu.com/p/4ce97b360c13 一.下载安装包 Webstorm 2017.1.4 百度云盘下载地址:https ...
- Laravel 根据任务的性质和要求决定处理的方式(Cron or Job)
1 前言 一般地,我们在应用的开发中,会碰到各种各样的任务解决需求.我的原则是,选择合适的方法做正确的事. 2 任务分类 在开发中, 一般会有以下几种性质的任务. 2.1 实时任务 一般是指,任务间的 ...
- spring 注解 注入属性 和 注解完成bean定义
1. 使用 @Autowired 和 @Resource 注解来注入属性 2. 使用 @Component.@Repository.@Service.@Controller 注解,就将该类定义为一个B ...
- Kendo UI中TreeView 放入tabstrip中,大数据量时超过边框的解决方案。
参考http://www.kendoui.com/forums/ui/tabstrip/tabstip-with-treeview-treeview-breaking-out-of-tabstrip. ...