Uva548——Tree
来源:互联网 发布:node即学即用 编辑:程序博客网 时间:2024/05/22 13:40
这题主要是根据中序遍历和后序变量构造二叉树。
一开始用 的是二叉树的数组表示形式来求解,但是本题的数据量挺大的,数组不能完全表示出来。关键点是这题的所有数都小于10000.所以用两个数组 lch 和rch 来表示权值点的左右孩子的情况。
然后在通过dfs来查找叶子结点到根节点的最小权值的叶子。
代码:
#include <iostream>#include <cstring>#include <sstream>#include <algorithm>using namespace std;int in_order[10010], post_order[10010];int lch[10100], rch[10100];int n;bool read_list(int *a){char line[100000];if(!cin.getline(line, 100000))return false;//cout << line << endl;stringstream ss(line);n = 0;int x;while(ss >> x){a[n++] = x;//cout << x << ' ';}return n > 0;}int build(int L1, int R1, int L2, int R2){if(L1 > R1)return 0;int root = post_order[R2];int p = L1;while(in_order[p] != root) p++;int num = p - L1;lch[root] = build(L1, p - 1, L2, L2 + num - 1);rch[root] = build(p + 1, R1, L2 + num, R2 - 1);return root;}int Min = 0xfffffff, node;void dfs(int u, int sum){sum += u;if(!lch[u] && !rch[u]){if(sum < Min || (sum == Min && u < node)){node = u;Min = sum;}}if(lch[u])dfs(lch[u], sum);if(rch[u])dfs(rch[u], sum);}int main(){//freopen("2.txt", "r", stdin);while(read_list(in_order)){read_list(post_order);build(0, n - 1, 0, n - 1);Min = 0xfffffff; node = 0;//for(int u = 1; u <= max; u++)//cout << tree[u] << ' ';//cout << endl;dfs(post_order[n - 1], 0);cout << node << endl;}return 0;}
0 0
- Uva548——Tree
- uva548 Tree
- UVA548 - Tree
- UVA548- Tree
- Tree UVA548
- uva548 - Tree
- tree uva548
- uva548 Tree
- uva548 -- Tree
- Uva548 Tree 二叉树遍历
- UVA548
- UVA548
- uva548
- uva548
- Uva548
- UVA548
- UVA548
- UVA548
- OpenCV解析SVM
- Tomcat+Spring web不能引入jquery
- 浙大PAT 1063
- OC笔记-贝塞尔曲线的应用
- 蓝桥杯—串逐位和(给定一个由数字组成的字符串,得到它的各个数位的和)
- Uva548——Tree
- PAT-1046划拳
- Linux 内存机制原理物理内存,Cache化,Buffer,Swap关系
- Spark学习笔记8-搭建spark的HA(用zookeeper实现spark的高可用)
- iOS 运行时2
- python中的文件
- Android性能优化典范(二)
- 分布式文件存储FastDFS(六)FastDFS多节点配置
- NSRunLoop 运行循环的粗浅理解;