uva 548 tree
来源:互联网 发布:c语言标识符命名规则 编辑:程序博客网 时间:2024/05/01 14:11
这题就是运用了二叉树重建, 以及遍历。
二叉树的遍历:先序遍历,中序遍历,后序遍历
只要有一个中序序列再加上另一个序列就可唯一地重建原来二叉树。
二叉树的遍历:先序遍历,中序遍历,后序遍历
只要有一个中序序列再加上另一个序列就可唯一地重建原来二叉树。
进行了二叉树重建之后,只要对这棵二叉树进行搜索, 取得各个路径之和,然后找出最小的那个和即可。
// Created by Chenhongwei in 2015.// Copyright (c) 2015 Chenhongwei. All rights reserved.#include"iostream"#include"cstdio"#include"cstdlib"#include"cstring"#include"climits"#include"queue"#include"cmath"#include"map"#include"set"#include"vector"#include"sstream"#include"algorithm"using namespace std;typedef long long ll;const int maxn=10010;int in[maxn],post[maxn],lch[maxn],rch[maxn],n;int ans=INT_MAX,ansp=INT_MAX;bool read(int* a){string s;if(!getline(cin,s))return false;stringstream ss(s);n=0;int x;while(ss>>x)a[n++]=x;return n>0;}int build(int l1,int r1,int l2,int r2){if(l1>r1)return 0;int root=post[r2];int p=l1;while(in[p]!=root)p++;int cnt=p-l1;lch[root]=build(l1,p-1,l2,l2+cnt-1);rch[root]=build(p+1,r1,l2+cnt,r2-1);return root;}void dfs(int u,int sum){sum+=u;if(!lch[u]&&!rch[u]){if(sum<ans||(sum==ans&&u<ansp)){ansp=u;ans=sum;}}if(lch[u])dfs(lch[u],sum);if(rch[u])dfs(rch[u],sum);}int main(){//ios::sync_with_stdio(false);// freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);while(read(in)){read(post);ans=INT_MAX,ansp=INT_MAX;build(0,n-1,0,n-1);dfs(post[n-1],0);cout<<ansp<<endl;}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
- 548 - Tree (UVa OJ)
- UVA 548 Tree
- eclipse为Android应用程序APK生成的私钥文件在哪里?
- Freeland团队暑期实践有感
- mysql出现权限问题无法创建新数据库&启动mysql失败问题,问题解决
- idea启动android项目时找不到已经运行的genymotion虚拟设备解决方案
- 遍历Map的四种方法
- uva 548 tree
- 星期二男孩问题
- 数据结构之链表实现
- 教你如何做个坏人----指令广播骚扰
- 小Y上学记——修学分(拓扑排序)
- 学习《算法导论》第十一章 散列表 总结一
- Hadoop压缩类型
- react-native试玩(16)-iOS分段控制控件
- c++ iterator(迭代器)分类及其使用