Tree UVA - 548

题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的。还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的。

最后输出来叶子节点。

一开始写的时候是用gets读入的,报CE,

要用fgets写,关于fgets(),传送门:

fgets函数及其用法,C语言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(二叉树路径最小值并且相同路径值叶子节点权值最小)的更多相关文章

  1. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  2. uva 548 Tree(通过后序,先序重建树+dfs)

    难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...

  3. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  4. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  5. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  6. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. UVa 548 Tree【二叉树的递归遍历】

    题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...

  8. UVa 548 Tree(二叉树最短路径)

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  9. UVa 548 Tree (建树+前序后序)

    Description You are to determine the value of the leaf node in a given binary tree that is the termi ...

随机推荐

  1. frame外弹出,刷新父页面

    //刷新父页面 function reflashParent() { var id = parent.tabbar.getActiveTab(); id = id.replace('tab','mai ...

  2. Cppcheck代码分析下

    1.流解析 解析函数中的可能的代码执行流, 函数实际执行中只会执行代码流中的一条流 分析: 分支语句 if-else ,switch-case         循环语句 while, do-while ...

  3. 元类编程-- 实现orm,以django Model为例

    # 需求 import numbers class Field: pass class IntField(Field): # 数据描述符 def __init__(self, db_column, m ...

  4. centos7.2编译安装zabbix-3.0.4

    安装zabbix-3.0.4 #安装必备的包 yum -y install gcc* make php php-gd php-mysql php-bcmath php-mbstring php-xml ...

  5. JDBC和Ibatis中的Date,Time,Timestamp处理

    在此前,遇到过使用Ibatis操作Oracle时时间精度丢失的问题,昨天又遇到JDBC操作MySQL时间字段的问题,从网上看到各种式样的解释这些问题的博文/帖子,但多是雾里看花,不得要领. 理解JDB ...

  6. 21、python操作redis的模块?

    什么是redis? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  7. Python 语言使用中遇到的问题汇总

    1.python中的传值和传引用 和其他语言不一样,传递参数的时候,python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方式.实际上,这种方式相当于传值和传 ...

  8. 我用.htaccess做了些什么?

    1.防图片盗链,减轻流量压力: 2.index.php 301转向到域名,有利于PR权重集中: 3.其它还不会,慢慢来…… 我是如何做的? <IfModule mod_rewrite.c> ...

  9. linux中断系统那些事之----中断处理过程【转】

    转自:http://blog.csdn.net/xiaojsj111/article/details/14129661 以外部中断irq为例来说明,当外部硬件产生中断时,linux的处理过程.首先先说 ...

  10. 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...