中序遍历
来源:互联网 发布:通信网络定义 编辑:程序博客网 时间:2024/05/22 12:50
#include<cstdio> ///中序遍历#include<cstdlib>#include<stack>using namespace std;typedef struct Node{ int k; Node *lchild,*rchild;}Node,*Link;Link p;int x;void create(Link& T){ scanf("%d",&x); if(x==-1) return ; T=(Link)malloc(sizeof(Node)); T->lchild=T->rchild=NULL; T->k=x; create(T->lchild); create(T->rchild);}void print(Link T)//递归 先序{ if(!T) return; printf("%d ",T->k); print(T->lchild); print(T->rchild);}// top pop push empty sizevoid stack_print(Link T)//栈 非递归中序{ Link p; stack<Link>s; s.push(T); while(!s.empty()){ while(p=s.top()) //向左走到尽头 s.push(p->lchild); s.pop(); // 空指针退栈 if(!s.empty()){ //向右 p=s.top(); s.pop(); printf("%d ",p->k); s.push(p->rchild); } }}void stack_print_2(Link T)//栈 非递归中序{ Link p; stack<Link>s; p=T; while(p||!s.empty()){ if(p){ //向左走到尽头 s.push(p);p=p->lchild; } else{ //向右 p=s.top(); s.pop(); printf("%d ",p->k); p=p->rchild; } }}int main(){ Link T=NULL; create(T); puts("递归先序遍历"); print(T); puts(""); puts("非递归中序遍历"); stack_print(T); puts(""); return 0;}/// 1 5 9 10 11 -1 -1 1 6 -1 -1 -1 -1 8 -1 -1 7 7 -1 -1 2 -1 -1
0 0
- 中序遍历
- 中前后序遍历
- 中序遍历
- 中序遍历
- 数据结构---中序遍历
- 中序遍历
- 中序遍历
- 先序遍历 中序遍历 后序遍历
- 数据结构之前序遍历,中序遍历,后序遍历
- 树 前序遍历,中序遍历,后序遍历
- 前序遍历 中序遍历 后序遍历
- 已知前序遍历和中序遍历,求后序遍历
- 二叉树前序遍历、中序遍历和后序遍历
- 已知前序遍历和中序遍历,求后序遍历
- 二叉树先序遍历、中序遍历、后序遍历
- 已知中序遍历和后序遍历,求前序遍历
- 已知后序遍历和中序遍历求前序遍历
- 先序遍历和中序遍历求后序遍历
- 利用霍夫变换检测图像中存在的圆与直线
- Hadoop
- hibernate总结
- 黑马程序员——IO流(二)
- POJ 1423 && HDU 1018 Big Number(数论)
- 中序遍历
- RAC资源调度
- armeabi和armeabi-v7a
- 软负载与nginx那些强大的不可不说的功能
- 网络-大文件下载(0621)
- [SHELL]20150621前总结——基本常用操作语句
- 异常总结
- [设计模式学习笔记]DECORATOR装饰模式
- 网络-监听 解析