UVa 536 Tree Recobery 二叉树重建
来源:互联网 发布:淘宝鸿星尔克优惠券 编辑:程序博客网 时间:2024/05/18 02:46
这个题看着比较简单就先做了,虽然用了不少时间,但是也学到了不少东西,对二叉树的遍历有了进一步的了解,然后就是提交答案的时候,有一些细节没有注意,提交了3次才过
思路:递归建立二叉树,用映射给节点编号,直到中序遍历建立二叉树结束为止,建立的同时输出后序遍历
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <map>using namespace std;char x[1024],z[1024],ltr[1024],rtr[1024];map<char,int> tree;void tmap(char s[]){for(int i=0;i<strlen(s);i++){tree[s[i]] = i;}}char build(int D1,int D2,int L1,int L2){if(L1>L2) return '0';char root=x[D1];int p = L1;while(z[p]!=x[D1]) p++;int cnt = p-L1;int troot = tree[root];ltr[troot] = build(D1+1,D1+cnt,L1,p-1);rtr[troot] = build(D1+cnt+1,D2,p+1,L2);cout<<root;return root;}int main(){ while(cin>>x>>z) { tree.clear(); memset(ltr,'0',sizeof(ltr)); memset(rtr,'0',sizeof(rtr)); tmap(x); int n = strlen(x); tree['0'] = -1; build(0,n-1,0,n-1); cout<<endl; } return 0;}
0 0
- UVa 536 Tree Recobery 二叉树重建
- uva 536 Tree Recovery(二叉树重建)
- 习题6-3 UVA 536 Tree Recovery 二叉树重建
- UVA 536 - Tree Recovery(二叉树重建)
- UVA 536 Tree Recovery 二叉树重建(重建二叉树+DFS)
- uva-536 二叉树重建
- UVa 548 - Tree 二叉树的重建与遍历
- UVa-536 习题6-3 二叉树重建(Tree Recovery,ULM 1997)
- 习题6-3 二叉树重建(Tree Recovery, ULM 1997, UVa 536)
- UVA 536 (二叉树重建 13.08.17)
- 二叉树重建(uva 536)
- 习题6-3 二叉树的重建 UVa 536
- JOJ 1329: Tree(重建二叉树)
- (二叉树重建)Tree Recovery(P2255)
- POJ2255,Tree Recovery,二叉树重建
- uva712 S-Tree (假二叉树重建)
- POJ 2255 Tree Recovery(二叉树重建)
- 二叉树重建(Tree Recovery ,UVa536)
- uboot之board.c
- JavaScript正则表达式——常用正则表达式整理
- 基于js的算术验证
- linux 5 clone to linux 6 libjava.so error
- C++ Handle(句柄) part1
- UVa 536 Tree Recobery 二叉树重建
- tomcat后端允许跨域访问的配置
- frame buffer驱动
- hive的查询注意事项以及优化总结
- 【Python】TCP Socket的粘包和分包的处理
- SQL语句的优先级(执行顺序)
- hdu 4148 规律题
- gulp常用插件使用
- Fragment设置主题