Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!
2024-08-27 13:55:30
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 ;
}
最新文章
- IOS
- Mastering Web Application Development with AngularJS 读书笔记-前记
- 160809209_李梦鑫_C语言程序设计实验2+选择结构程序设计_进阶
- code vs1506传话(塔尖)+tarjan图文详解
- 最长递增子序列 O(NlogN)算法
- Java int to String互转
- laravel 5.3 学习之路——路由(资源,别名)
- const与#define宏常量 , inline与#define
- DEV--GerdView控件
- lintcode:字符串置换
- URL重写 UrlRewrite
- JQuery 补充
- UIImageView帧动画相关属性和方法
- 小白浅论JAVA数组中“for加强版”
- springboot mybatis 整合
- 关于SQL的over partition by 开窗语句在分页和统计中的使用总
- 1.Django入门
- CDH上Cloudera Management Service 各个角色迁移至其他节点
- NSString 和 NSData 转换
- Python爬虫之requests库介绍(一)