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 ...
随机推荐
- Iterative (non-recursive) Merge Sort
An iterative way of writing merge sort: #include <iostream> using namespace std; void merge(in ...
- JS 导出Table为excel的三种可行方法
[html] view plain copy<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...
- 点击tablecell中的一个按钮,确定cell所在的行
- (void) del:(UIButton *) button { NSLog(@"%s",__FUNCTION__); UITableViewCell * cell = (UI ...
- 将NSArray反向排序
NSArray * array = [NSArray arrayWithObjects:", nil]; NSArray * reverseArray = [[array reverseOb ...
- 转:使用rsync在linux(服务端)与windows(客户端)之间同步
转自:http://blog.csdn.net/old_imp/article/details/8826396 一 在linux(我用的是centos系统)上安装rsync和xinetd前先查看lin ...
- Python script to create Screen from all Items/Graphs of a host
#!/usr/bin/env python import urllib2 import json import argparse def authenticate(url, username, pas ...
- Wd 西部数据
西部数据 https://item.jd.com/3564471.html#none 打算买一个大硬盘记录代码片段.开发项目.开发工具.电影游戏等…… /** * 获取100天后的日子 * 用来做计划 ...
- 如何理解OOP?
OOP (Object Oriented Programming)面向对象编程 1.它符合我们现在思考习惯 2.它让一些复杂的事情变得更加简单 3.它让操作者比那成了指挥者
- 【Python + Selenium】之JS定位总结
感谢:小琰子 Python+Selenium 脚本中的一些js的用法汇总: 1.滚动条 driver.set_window_size(500,500) js = "window.scroll ...
- 我在面试.NET/C#程序员时会提出的问题(转载)
转自:http://blog.zhaojie.me/2011/03/my-interview-questions-for-dotnet-programmers.html 说起来我也面试过相当数量的.N ...