UVA 548 Tree 建树
题意:
输入中序和后序的权值,输出哪个叶子使它到根的路径上权和最小。
思路:
输入后建树,然后dfs求最小的叶子。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<sstream>
using namespace std; int mid[], post[];
int judge[];
struct node {
int index;
node *left, *right;
node()
{
left = right = NULL;
}
};
int n;
bool input(int* a)//输入函数
{
string line;
memset(a, , sizeof(a));
if (!getline(cin, line))
return false;
stringstream ss(line);
int x;
n = ;
while (ss >> x)
a[n++] = x;
//printf("n=%d\n", n);//
return n>;
} int cnt, res;
node* build(node* root, int l, int r)
{
int i;
int t = post[--cnt];
for (i = l; i<r; i++)
{
if (t == mid[i])
break;
}
root = new node();
root->index = t;
/* 注意下面是先建右边然后建左边
因为后序往前走(--cnt) */
if (i<r - )
root->right = build(root->right, i + , r);
if (i>l)
root->left = build(root->left, l, i); return root;
} void preorder(node *root)
{
printf("%d ", root->index);
if (root->left != NULL)
preorder(root->left);
if (root->right != NULL)
preorder(root->right);
}
int best, best_num;
void dfs(node *root, int sum)
{
sum += root->index;
if (!root->left && !root->right)
{
if (best_num>sum || (sum == best_num&&root->index<best))
{
best = root->index;
//printf("best=%d\n", best);
best_num = sum;
}
}
if (root->left != NULL)
dfs(root->left, sum);
if (root->right != NULL)
dfs(root->right, sum);
} int main()
{
while (input(mid))
{
input(post);
cnt = n;
//printf("cnt=%d\n", cnt);
memset(judge, , sizeof(judge));
node *root = NULL;
root = build(root, , n);
best_num = ;
//preorder(root);
//printf("\n");
dfs(root,);
printf("%d\n",best);
}
return ;
}
UVA 548 Tree 建树的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- 【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...
- 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
0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
随机推荐
- (转)史上最全 40 道 Dubbo 面试题及答案,看完碾压面试官!
背景:因为自己的简历写了dubbo,面试时候经常被问到.实际自己对dubbo的认识只停留在使用阶段,所以有必要好好补充下基础的理论知识. https://zhuanlan.zhihu.com/p/45 ...
- MySQL5.7.23解压版安装教程
每次找安装教程太麻烦,因此给自己备份一下步骤,方便以后查看.解压版下载地址https://dev.mysql.com/downloads/mysql/,详细图解如下: 1.根据自己需求,选择适合自己的 ...
- Java中多态性的实现
class A ...{ public String show(D obj)...{ return ("A and D"); } public String show(A obj) ...
- Nginx 站点设置目录列表显示
Nginx 站点目录列表显示. 可以编辑添加在 server { } 模块 或者 location { } 模块下. autoindex on; # 开启目录文件列表 autoindex_exact_ ...
- hibernate之Configuration对象
任务:读取主配置信息 1. Configuration config = new Configuration(); 使用hibernate,但并没有读取 2. config.config ...
- 在centos7下安装.net core
在这里记录下安装的过程: 一开始需要去官网下载centos相关的dotnetcore的sdk 上传到linux,解压,发现需要安装libunwind, 安装libunwind:yum install ...
- Eclipse编译运行没问题,但执行mvn clean install跑单元测试失败的原因解析
问题描述:mvn clean install编译工程并运行单元测试出现如下错误 Tests run: 3, Failures: 0, Errors: 2, Skipped: 0, Time elaps ...
- 其他-pkuwc2019数学考试题目
时限150min,有windows和Ubuntu使用 十道填空题,在poj上举行,选手提交答案,系统将答案自动填入一个作用是输出答案的程序,再将该程序提交评测(由于该程序变量名为longlong,所以 ...
- Lua中__index和__newindex实践
[具有默认值的table] 我们都知道,table中的任何字段的默认值都是nil,但是通过元表,我们可以很容易的修改这一规定,代码如下: function setDefault(tb, default ...
- 帆软报表(finereport)安装/配置
1.首先是安装帆软报表软件 下载地址:http://www.finereport.com/product/download 激活码注册格账号就有了 2.启动软件,新建连接数据库 点 ...