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 ...
随机推荐
- SDUTOJ 2804求二叉树的深度
#include<iostream> #include<stdlib.h> #include<string.h> using namespace std; char ...
- JavsScript中DOM的基本操作
节点及其类型 元素节点 属性节点: 元素的属性, 可以直接通过属性的方式来操作. 文本节点: 是元素节点的子节点, 其内容为文本. 在 html 文档的什么位置编写 js 代码 直接在 html 页面 ...
- MySQL 字符编码总结
今天操作服务器数据库时遇到了Mysql中文字符乱码的问题,主要原因是因为安装的时候没有设置好字符集. 很是郁闷,因为库里有很多重要数据,所以重装是不可能了,于是决定找找在不重装且不改代码的前提下,能搞 ...
- KingdeeK3-修改单据邮件发送的自定义字段
只需要执行类似下面语句即可: update ICTemplateentry set FVisForBillType = 63,flookupcls=6 where fid ='P01' and fhe ...
- Java除法结果带小数、进一法的实现 Java问题通用解决代码
http://blog.csdn.net/windone0109/article/details/5355379进一法: 即省略的位上只要大于零都要进一位 : 四舍五入法: 即省略的位上小于五都要舍 ...
- cobbler pxe-menu
对应的文件在 /var/lib/tftpboot/pxelinux.cfg下 如果profile的pxe-menu设置为1的话,就可以默认显示在menu上了.可以手动选择要下发哪一个profile. ...
- jquery插件2
1.很全,好用的jquery插件库:http://www.jq22.com/ 2.素材:http://www.sucaijiayuan.com/ 3.不错:http://www.helloweba.c ...
- web安全之SQL注入---第二章 什么是sql注入?
如何理解SQL注入?SQL注入是一种将SQL代码添加到输入参数中传递到SQL服务器解析并执行的一种攻击方法总结:其实就是输入的参数没有进行过滤,直接参加sql语句的运算,达到不可预想的结果.SQL注入 ...
- windows中wamp环境composer使用中openssl问题解决
今天在windows下学习lavaral,使用composer update命令报如下错误: [Composer\Exception\NoSslException] The openssl exten ...
- poj 3468 Splay 树
大二上的时候.写过一个AVL的操作演示,今天一看Splay.发现和AVL事实上一样,加上线段树的基础,懒惰标记什么都知道.学起来轻松很多哦 我參考的模板来自这里 http://blog.csdn.n ...