UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548

题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的。还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的。
最后输出来叶子节点。
一开始写的时候是用gets读入的,报CE,
要用fgets写,关于fgets(),传送门:
一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs。。。
代码:
//二叉树的中序和后序遍历还原树并输出最短路径的叶子节点值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f; struct node{
int l,r,val,father;
}tree[maxn]; int n=,minn,leaf;
int mid[maxn],beh[maxn]; int build(int la,int ra,int lb,int rb)
{
if(la>ra) return ;
int rt=beh[rb];
int p1=la;
while(mid[p1]!=rt) p1++;
int p2=p1-la;
tree[rt].l=build(la,p1-,lb,lb+p2-);
tree[rt].r=build(p1+,ra,lb+p2,rb-);
return rt;
} /*
int fa[maxn]; void bfs(int x)
{
queue<int> q;
vector<int> vec;
q.push(x);
fa[x]=0;tree[0].val=0;
while(!q.empty()){
int rt=q.front();q.pop();
if(rt==0){
break;
}
tree[rt].val=tree[fa[rt]].val+rt;
// cout<<tree[rt].val<<endl;
if(tree[rt].l){
fa[tree[rt].l]=rt;
q.push(tree[rt].l);
}
if(tree[rt].r){
fa[tree[rt].r]=rt;
q.push(tree[rt].r);
}
if(tree[rt].l==0&&tree[rt].r==0){
vec.push_back(rt);
} }
int l=vec.size();
int minn=inf,ans=inf;
for(int i=0;i<l;i++){
if(tree[vec[i]].val<minn){
minn=tree[vec[i]].val;
ans=vec[i];
}
if(tree[vec[i]].val==minn){
if(vec[i]<ans) ans=vec[i];
}
}
printf("%d\n",ans);
}
*/ void dfs(int rt,int sum)
{
sum+=rt;
if(!tree[rt].l&&!tree[rt].r){
if(sum<minn||(sum==minn&&rt<leaf)){
minn=sum;
leaf=rt;
}
}
if(tree[rt].l) dfs(tree[rt].l,sum);
if(tree[rt].r) dfs(tree[rt].r,sum);
} char c[maxn]; int main()
{
while(fgets(c,maxn,stdin)){
int l=strlen(c);
int i=,cnt=;
if(n==){
while(i<l){
if(c[i]=='\0') break;
if(c[i]==' '||c[i]=='\n') i++,mid[++n]=cnt,cnt=;
else{
cnt=cnt*+c[i]-'';
i++;
}
}
}
else{
n=;
while(i<l){
if(c[i]=='\0') break;
if(c[i]==' '||c[i]=='\n') i++,beh[++n]=cnt,cnt=;
else{
cnt=cnt*+c[i]-'';
i++;
}
}
build(,n,,n);
// bfs(beh[n]);
minn=inf,leaf=beh[n];
dfs(beh[n],);
printf("%d\n",leaf);
n=;
}
}
}
UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】
[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
随机推荐
- jsoup select 选择器
转载自:http://blog.csdn.net/zhejingyuan/article/details/11801027 方法 利用方法:Element.select(String selector ...
- 51Nod 1004 n^n末尾数字 | 快速幂
#include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...
- 【BZOJ1085】【SCOI2005】骑士精神 [A*搜索]
骑士精神 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的 ...
- python初步学习-查看文档及数据类型转换
学会查看文档 python最权威的文档就是官方文档,因此,我们要学会查看官方文档.而在python解释器中,help() 命令就可以很权威的查看官方文档. 因此,我们要学会使用 help() 命令. ...
- 【HNOI】矩阵染色 数论
[题目描述]一个2*i的矩阵,一共有m种颜色,相邻两个格子颜色不能相同,m种颜色不必都用上,f[i]表示这个答案,求Σf[i]*(2*i)^m (1<=i<=n)%p. [数据范围] 20 ...
- velocity & freemarker
一.Velocity Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅使用简单的模板语言(template language)来引用由java代码定义的 ...
- spring boot 注解说明
Starters 可以创建自己的Starter,但名字格式不能是 spring-boot-starter-*,而是 *-spring-boot-starter.类似Maven插件的规则. 自动配置 ...
- Development tools[重点]
Development tools yum groupinfo "Development tools" Loaded plugins: product-id, security, ...
- 单从软件层面来说,Maya 和 Blender 差别在哪?
单从软件层面来说,Maya 和 Blender 差别在哪? https://www.zhihu.com/question/21975571
- Redis 3.0 编译安装
Redis 3.0 编译安装 http://www.xuchanggang.cn/archives/991.html