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 ...
随机推荐
- Notepad++搭配MinGW 配置编译运行C/C++
1. Notepad++与Dev-Cpp都能编译运行, 环境变量的设置: 在PATH中加入"Dev-Cpp的MinGW64下的bin", 这是寻找gcc编译器的路径. 新建LIBR ...
- UITableViewCell中的UILabel添加手势没有响应的解决方法
有时候自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势.但是如果按照常规的添加方法,发现所添加的手势并不能响应.以下为解决方法:将手势 ...
- AIX 安装标准
文件夹 一.网卡需求 二.光纤卡需求 三.磁盘需求 四.主机文件系统需求 五.主机名命名规范 六.安装设置规范 七.參数改动规范 八.时钟同步设置 九.rootvg做镜像 十.AIX系统安全加固 一. ...
- [j2ee]java中的xml操作
一.XML简单介绍 xml是可扩展标记语言,主要用来标记数据.定义数据类型,很适合万维网传输. xml特点: xml是一种标记语言.非常类似HTML xml的设计宗旨是数据传输,而不是显示数 ...
- PyQt5 Function Parameter Declaration
addWidget self.lcd = QLCDNumber() grid.addWidget(self.lcd,0,0,3,0) grid.setSpacing(10) void QGridLay ...
- (三)spark算子 分为3大类
ation算子通过sparkContext执行提交作业的runJob,触发rdd的DAG执行 (foreach) foreach(f) 会对rdd中的每个函数进行f操作,下面的f操作就是打印输出没有元 ...
- Weka学习之认识weka(一)
Weka 简介 WEKA作为一个公开的数据挖掘工作平台,集合了大量能承担数据挖掘任务的机器学习算法,包括对数据进行预处理,分类,回归.聚类.关联规则以及在新的交互式界面上的可视化. Weka是基 ...
- memcached 输入命令后无法启动
键入命令启动memcached服务器,没有任何反应,使用telnet 127.0.0.1 11211 也是无法链接. memcached -d -m -u root -l -c -P /tmp/mem ...
- 【整理】Android中EditText中的InputType类型含义与如何定义( 转 )
转自:[整理]Android中EditText中的InputType类型含义与如何定义 用到的时候查到了这篇文章觉得很不错,就在此记录下. [背景] 经过一些Android中EditText方面的折腾 ...
- 通过Lock对象以及Condition对象实现多线程同步
通过Lock对象以及Condition对象实现多线程同步: 在之前的学习中,无论是通过synchronized建立同步代码块,还是通过synchronized建立同步函数,都是把对象看成一把锁来实现同 ...