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 <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
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 print(node *root) {
if (!root) return;
node *cur = root;
while (cur) {
if (!cur->left) {
cout << cur->data << " ";
cur = cur->right;
}
else {
node *pre = cur->left;
while (pre->right && pre->right != cur) pre = pre->right;
if (pre->right == NULL) {
pre->right = cur;
cur = cur->left;
}
else {
pre->right = NULL;
cout << cur->data << " ";
cur = cur->right;
}
}
}
} void prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} int main() {
node* root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
print(root);
return ;
}
Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!的更多相关文章
- 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: Iterative Postorder Traversal
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...
- Data Structure Binary Tree: Morris traversal for Preorder
http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...
- 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: Populate Inorder Successor for all nodes
http://www.geeksforgeeks.org/populate-inorder-successor-for-all-nodes/ #include <iostream> #in ...
- Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals
http://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/ #include < ...
- 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 Search Tree: Inorder Successor in Binary Search Tree
struct node { int val; node *left; node *right; node *parent; node() : val(), left(NULL), right(NULL ...
- Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合
给定一个二叉树,以集合方式返回其中序/先序方式遍历的所有元素. 有两种方法,一种是经典的中序/先序方式的经典递归方式,另一种可以结合栈来实现非递归 Given a binary tree, retur ...
随机推荐
- view无限旋转
- (void) showRefreshAnimation { [UIView animateWithDuration: options:UIViewAnimationOptionCurveLinea ...
- PJISP 修改 消息头Fromto字段
项目需求,需要修改sip信令消息头中Fromto字段,完成此功能需要修改sip库(PJSIP)源码,具体如下: PJSIP 消息头 Formto 字段默认的格式是sip:平台@平台IP地址,例如si ...
- 下拉刷新Listview(8.30)
Android-PullToRefresh 1项目托管地址: https://github.com/bavariama1/Android-PullToRefresh 2 快速开始教程:https:// ...
- 匿名函数 invoke
delegate string MyDele(string str); string MyFun(string str) { return str; } private void Form1_Load ...
- karma + phantom + mocha + sion + chai + nightwatch + selenium2(webdriver) 测试框架学习
第三方的教程传送门 https://segmentfault.com/a/1190000004558796 karma # github https://github.com/karma-runner ...
- hdu 1811 Rank of Tetris(拓扑,并查集)
题意:略 分析:排序先按rating,若相同,则按rp.考虑到每个人的rp均不同,所以rating相同的人必然可以排序.那么只需要考虑rating不同的集合了. 大小关系可以用有向边表示,而大小关系 ...
- SecureCRT 设置字体跟颜色
SecureCRT 绝佳配色方案, 保护你的眼睛 分类: Linux 软件使用2013-05-17 08:45 24038人阅读 评论(11) 收藏 举报 SecureCRT 绝佳配色方案, 保护你的 ...
- JSP 表单处理向服务器提交信息
JSP 表单处理 我们在浏览网页的时候,经常需要向服务器提交信息,并让后台程序处理.浏览器中使用 GET 和 POST 方法向服务器提交数据. GET 方法 GET方法将请求的编码信息添加在网址后面, ...
- 从sql走向linq的我撞死在起点上
[本文纯个人理解,错误轻喷,非常希望能有大神指点] A left (outer) join B on A.bid=B.id 上面这句话叫做左连接,原因是left(左)join(加入,连入)被译为左连接 ...
- 数字证书转换cer---pem
下载openssl-1.0.1s 安装好openssl之后,进入openssl目录: 输入openssl命令,即进入命令模式: 先将要转换的cer证书也放到openssl目录下面,然后执行以下 ...