Data Structure Binary Tree: Iterative Postorder Traversal
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void prints(node *root) {
if (!root) return;
stack<node*> S;
do {
while (root) {
if (root->right) S.push(root->right);
S.push(root);
root = root->left;
}
root = S.top();
S.pop();
if (root->right && !S.empty() && root->right == S.top()) {
S.pop();
S.push(root);
root = root->right;
}
else {
cout << root->data << " ";
root = NULL;
}
} while (!S.empty());
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->right->right = new node();
prints(root);
return ;
}
Data Structure Binary Tree: Iterative Postorder Traversal的更多相关文章
- Data Structure Binary Tree: Level order traversal in spiral form
http://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/ #include <iostream> #includ ...
- Data Structure Binary Tree: Morris traversal for Preorder
http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...
- Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...
- Data Structure Binary Tree: Boundary Traversal of binary tree
http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/ #include <iostream> #include & ...
- Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!
http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/ #include &l ...
- Data Structure Binary Tree: Inorder Tree Traversal without Recursion
http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/ #include <iostream> #in ...
- Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree
http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/ #include <iostream> #in ...
- Data Structure Binary Tree: Connect nodes at same level using constant extra space
http://www.geeksforgeeks.org/connect-nodes-at-same-level-with-o1-extra-space/ recursive: #include &l ...
- Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion
http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/ #include ...
随机推荐
- java 根据生日计算年龄 Java问题通用解决代码
根据生日计算年龄可以通过Calendar实现.最简单可以考虑get(Calendar.DAY_OF_YEAR)来简单修正年龄,但是遇到生日在闰年的2月29之后,或者今年是闰年的2月29之后可能出现计算 ...
- svn上传代码时出现:Authorization failed
出现该问题基本都是三个配置文件的问题,下面把这个文件列出来 svnserve.conf:[general]anon-access = readauth-access = writepassword-d ...
- eclipse中代码没错但项目名称有个小红X
快速找到项目中的错误,eclipse程序> window> show View >problems ;选择后看控制台报的错误,你就知道什么原因出小红X了
- apk文件反编译
apk文件的反编译,需要的工具apktool(反编译资源文件)和dex2jar-0.0.7.9-SNAPSHOT(反编译源码) 1. 下载相关软件 1)Apktool,下载地址:http://cod ...
- Mysql查询结果只有一条的情况下把值赋值给变量,再用if else 流程判断
BEGIN set @n=(SELECT count(day) from log where day=CURDATE()); THEN call m_LogInsert(); ELSE call m_ ...
- 【0】按照Django官网:实现第一个django app 安装必要的工具/模块
1.环境配置: (1)Install Setuptools¶ To install Python packages on your computer, Setuptools is needed. Do ...
- windows和linux下目录分隔符兼容问题(换行回车兼容)
windows和linux下目录分隔符兼容 DIRECTORY_SEPARATOR 换行回车兼容 PHP_EOF
- Struts2学习之拦截器栈
© 版权声明:本文为博主原创文章,转载请注明出处 拦截器栈: - 从结构上看:拦截器栈相当于多个拦截器的组合 - 从功能上看:拦截器栈也是拦截器 默认拦截器栈: - 在struts-core.jar中 ...
- 为什么 java wait/notify 必须与 synchronized 一起使用,jvm究竟做了些什么
这个课题提出来的是原先的线程并发解决的思路.目前解决线程并发,可以是lock接口结合condition 并发问题一直以来就是线程必不可少的话题. java 是第一个内置对多线程支持的主流编程语言.在 ...
- [转]maven2中snapshot快照库和release发布库的应用
[转载声明] 转载时必须标注:本文来源于铁木箱子的博客http://www.mzone.cc [原文地址] 原文永久地址是:http://www.mzone.cc/article/279.html 在 ...