数据结构实验之求二叉树后序遍历和层次遍历
2014-11-05 22:22
337 查看
#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
& %llu
Submit Status
Description
已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历。
Input
输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。
Output
每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列
Sample Input
2 abdegcf dbgeafc xnliu lnixu
Sample Output
dgebfca abcdefg linux xnuli
相关文章推荐
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历(简单版)
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历 分类: 树 2015-06-21 10:58 11人阅读 评论(0) 收藏
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构实验之求二叉树后序遍历和层次遍历