Data Structure Binary Tree: Iterative Postorder Traversal
2024-09-08 14:42:50
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 ;
}
最新文章
- [翻译]ES 提案: global
- 【转载】wireshark:no interface can be used for capturing in this system with the current configuration
- sql里Where条件顺序
- 英文论文写作之讨论与结论Discussion and Conclusion
- Android 源码编译 步骤
- mysql登陆报错(ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2))
- Boost.Asio c++ 网络编程翻译(14)
- Oracle基本流程语句
- 无法打开物理文件mdf,操作系统错误 5:&;quot;5(拒绝訪问。)&;quot;
- 开涛spring3(7.3) - 对JDBC的支持 之 7.3 关系数据库操作对象化
- J2EE进阶(十)SSH框架整合常见问题汇总(一)
- qt 视频播放器错误解决方法
- PostgreSQL学习笔记(二)-安装pgAdmin
- decode函数解决oracle报错";除数为0";的问题
- Arrays和String单元测试
- 创建react项目
- MonGoDB 常见操作, 设置管理员和用户登入
- 常用算法1 - 快速排序 &; 二分查找
- 六种流行的语言大餐---C、C++、python、Java、php、C#你更喜欢哪一个呢?
- Php与Erlang的Socket通信
热门文章
- EFCore &; Mysql migration on Production
- shell脚本检测网络是否畅通
- Kafka备忘
- 2.JAVA编程思想——一切都是对象
- PHP运行环境之IIS FastCGI 进程意外退出解决办法
- codeforces 427 div.2 F. Roads in the Kingdom
- android 国际化 设置
- IOS连接
- 计算点与x轴正半轴夹角atan2(double y,double x),返回弧度制(-PI,PI]
- [Sdoi2013]直径(树的直径)