UVA 548-Tree
来源:互联网 发布:淘宝详情关联怎么做 编辑:程序博客网 时间:2024/06/05 15:19
UVA 548-Tree
题目大意:给出一棵树的中序遍历和后序遍历,求出路径上权之和最少的路
解题思路:由后序遍历可得到头节点,然后用中序遍历利用递归不断分割,建好树后递归找最小值
#include <iostream>#include <sstream>#include <string>using namespace std;const int maxi = 10000 + 100;int mid[maxi], post[maxi];int Lro[maxi], Rro[maxi];int mi;int re;int read(int *m) { string l; int a; int n = 0; getline(cin, l); stringstream ss(l); while(ss >> a) m[n++] = a; return n;}int tree(int L1, int R1, int L2, int R2) { if(L1 > R1) { return 0; } int root =post[R2]; int i = L1; for(; mid[i] != root; i++); int num = i - L1; Lro[root] = tree(L1, i - 1, L2, L2+num-1); Rro[root] = tree(i+1, R1, L2 +num, R2-1); return root;}void find(int root, int sum) { sum = sum + root; if(Lro[root] == 0 && Rro[root] == 0) { if(sum < mi || (sum == mi && root < re)) { mi = sum; re = root; } } else { if(Lro[root] != 0) find(Lro[root], sum); if(Rro[root] != 0) find(Rro[root], sum); }}int main() { while(read(mid)) { int ro; mi = 100000; int n = read(post); ro = tree(0, n-1, 0, n-1); find(ro, 0); cout << re << "\n"; } return 0;}
0 0
- UVA 548-Tree
- UVa 548 Tree
- uva 548 Tree
- uva 548 - Tree
- uva 548 - Tree
- UVa 548 - Tree
- uva 548 Tree
- uva 548 - Tree
- 数据结构 uva 548-Tree
- UVA 548 - Tree
- UVa 548 - Tree
- UVa 548 - Tree
- uva-548 Tree
- UVA 548 Tree
- UVA 548 tree
- uva 548 -Tree
- uva 548 Tree
- 548 - Tree (UVa OJ)
- caffe源码阅读9-loss_layer.hpp+各cpp
- 网心科技李浩:通过技术创新提高直播体验
- R语言基本图形绘制
- 导入Excel方式汇总--第二种
- Markdown如何满足中文阅读习惯,实现首行缩进?
- UVA 548-Tree
- centos7 设置本地光盘yum源
- poj 2151 Check the difficulty of problems(概率dp)
- 数据存储方式
- 手把手教你提高英语能力
- java编程思想 bruce Eckel chapter10 内部类
- 编程之美3.7队列中取最大值操作问题Java版
- 数据库分库分表
- 【Android联系人】仿小米联系人(MIUI8)