UVA536 Tree Recovery
来源:互联网 发布:sql on hadoop 编辑:程序博客网 时间:2024/06/05 19:19
基础到不能再基础的。。数据结构题?(雾)
pre_order的字符串找到树根,然后在in_order中找到树根,将树划分为左右子树。。递归求解即可。
AC代码如下。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int left[26];int right[26];char pre_order[26];char in_order[26];int build(int pre_start,int pre_end,int in_start,int in_end){int root=pre_order[pre_start]-'A';if(pre_end-pre_start==1) return pre_order[pre_start]-'A';if(pre_end<=pre_start) return -1;int mid=find(&in_order[in_start],&in_order[in_end],pre_order[pre_start])-in_order;int pos=pre_start;for(int i=in_start;i<mid;i++) pos=max(pos,(int)(find(&pre_order[pre_start],&pre_order[pre_end],in_order[i])-pre_order)); left[root]=build(pre_start+1,pos+1,in_start,mid); right[root]=build(pos+1,pre_end,mid+1,in_end); return root;}void print(int root){if(root==-1)return;else{print(left[root]);print(right[root]);printf("%c", root+'A');}}int main(){while(scanf("%s",pre_order)!=EOF){memset(right,-1,sizeof(right));memset(left,-1,sizeof(left));scanf("%s", in_order);int l=strlen(in_order);int root=build(0,l,0,l);print(root);printf("\n");}return 0;}
0 0
- uva536 Tree Recovery
- uva536 Tree Recovery
- UVA536 - Tree Recovery
- UVa536 Tree Recovery
- uva536 Tree Recovery
- UVA536 Tree Recovery
- uva536 -- Tree Recovery
- Tree Recovery UVA536
- UVA536 - Tree Recovery(递归)
- Uva536——Tree Recovery
- 二叉树重建(Tree Recovery ,UVa536)
- uva536-Tree Recovery-二叉树遍历
- 习题6-3 UVa536 Tree Recovery(树的遍历转换)
- Uva536 Tree Recovery 【递归建树】【习题6-3】
- [UVA548]Tree[根据中序后序构建树][UVA536]Tree Recovery[根据前序中序构建树]
- 算法竞赛入门经典 第二版 习题6-3 二叉树重建 Tree Recovery uva536
- uva536
- uva536
- velocity
- 动态内存分配的大家族
- OpenCV-3.模版匹配
- 985的数学难题
- 10个超棒jQuery表单操作代码片段
- UVA536 Tree Recovery
- android 解决 Popupwindow不能复制粘贴的问题
- Nexus私服搭建
- 动态规划-Dynamic Process
- http协议学习系列(https通信过程)
- opencv之fillpoly
- c++中enum 如何使用
- DirectShow 播放mp4编码 视频
- ios之Windows环境下开发ios环境部署