http://acm.nyist.net/JudgeOnline/problem.php?pid=221
来源:互联网 发布:网络推广课程培训班 编辑:程序博客网 时间:2024/05/16 12:57
已知一棵树的先序和中序遍历,求该树的后序遍历,,,
例如:
DBACEGF ABCDEFG
ACBFGED
AC代码:
#include<stdio.h>#include<string.h>void build(int n,char *s1,char *s2)//构造后序遍历过程{if(n<=0) return;int p=strchr(s2,s1[0])-s2;build(p,s1+1,s2);//访问左子树build(n-p-1,s1+p+1,s2+p+1);//访问右子树printf("%c",s1[0]);}int main(){char a[27],b[27];while(scanf("%s%s",a,b)==2){int n=strlen(a);build(n,a,b);printf("\n");}return 0;}
法二:
#include<iostream>#include<string>#include<algorithm>using namespace std;typedef struct str{char date;struct str *l,*r;}*Tire,T;Tire build(string s,string s1){ Tire u=NULL; if(s.size()>0) {u=new T;u->date=s[0];int k=s1.find(s[0]);u->l=build(s.substr(1,k),s1.substr(0,k));u->r=build(s.substr(k+1),s1.substr(k+1)); } return u;}void delet(Tire root){if(root->l) delet(root->l);if(root->r) delet(root->r);delete root;}void postorder(Tire root){if(root->l) postorder(root->l);if(root->r) postorder(root->r);cout<<root->date;}int main(){string a,b;while(cin>>a>>b){Tire root=build(a,b);postorder(root);cout<<endl;delet(root);}return 0;}
- http://acm.nyist.net/JudgeOnline/problem.php?pid=221
- http://acm.nyist.net/JudgeOnline/problem.php?pid=290
- http://acm.nyist.net/JudgeOnline/problem.php?pid=434
- http://acm.nyist.net/JudgeOnline/problem.php?pid=13
- http://acm.nyist.net/JudgeOnline/problem.php?pid=239
- http://acm.nyist.net/JudgeOnline/problem.php?pid=129
- http://acm.nyist.net/JudgeOnline/problem.php?pid=431
- http://acm.nyist.net/JudgeOnline/problem.php?pid=301
- http://acm.nyist.net/JudgeOnline/problem.php?pid=451
- http://acm.nyist.net/JudgeOnline/problem.php?pid=459
- http://acm.nyist.net/JudgeOnline/problem.php?pid=20
- http://acm.nyist.net/JudgeOnline/problem.php?pid=267
- http://acm.nyist.net/JudgeOnline/problem.php?pid=305
- http://acm.nyist.net/JudgeOnline/problem.php?pid=309
- http://acm.nyist.net/JudgeOnline/problem.php?pid=325
- http://acm.nyist.net/JudgeOnline/problem.php?pid=420
- http://acm.nyist.net/JudgeOnline/problem.php?pid=488
- http://acm.nyist.net/JudgeOnline/problem.php?pid=150
- Boost库单元测试使用配置
- C++覆盖、重载和隐藏的区别
- 再次写给我们这些浮躁的程序员
- 利用powerdesigner反向数据库结构,生成ER图
- POJ 1922 Ride to School
- http://acm.nyist.net/JudgeOnline/problem.php?pid=221
- frameset标签
- GDB 调试
- Linux编译链接环境变量
- Oracle调研常用DML
- 微软Win7系统安装字体技巧
- C文件读写函数选用
- android copybit的应用
- 部署windows服务