数据结构实验之求二叉树后序遍历和层次遍历
来源:互联网 发布:网络维护的工作内容 编辑:程序博客网 时间:2024/05/01 12:44
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>using namespace std;struct node{ char date; node *l,*r;};node *build(char *a,char *b,int n){ node *t; if(n<=0) { return NULL; } t=new(node); t->date=a[0]; int i; for(i=0;i<n;i++) { if(a[0]==b[i]) { break; } } t->l=build(a+1,b,i); t->r=build(a+i+1,b+1+i,n-i-1); return t;}void last(node *t){ if(t==NULL) { return ; } last(t->l); last(t->r); cout<<t->date;}void cengci(node *t){ node *p[10001]; int in=0,out=0; p[in++]=t; while(in>out) { t=p[out++]; if(t!=NULL) { cout<<t->date; p[in++]=t->l; p[in++]=t->r; } }}int main(){ char a[10001],b[10001]; int y; cin>>y; while(y--) { node *tree=NULL; cin>>a>>b; int n=strlen(a); tree=build(a,b,n); last(tree); cout<<endl; cengci(tree); cout<<endl; } return 0;}
数据结构实验之求二叉树后序遍历和层次遍历
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %lluDescription
已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历。
Input
输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。
Output
每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列
Sample Input
2abdegcfdbgeafcxnliulnixu
Sample Output
dgebfcaabcdefglinuxxnuli
0 0
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- Ubuntu 12.04 安装 Apache2+PHP5+MySQL
- Ubuntu图形界面环境下启动应该程序:
- 学霸是怎样炼成的(大一上篇)
- eclipse优化以及jvm内存的讲解与设置
- 串行口API的使用
- 数据结构实验之求二叉树后序遍历和层次遍历
- Android突破二之WindowManager、Display、DisplayMetrics类
- Diameter协议
- 如果当初学习编程时能有人给我这些忠告该多好
- oracle中to_char后查询当天在今年的第几周ww和iw的区别
- 15个你可能不知道的开源云平台
- Unity3d TweenPosition.Begin()的使用浅析
- ios Asynsocket 接收数据委托函数不执行
- 【Leetcode】Add two numbers JAVA