题意:

输入中序和后序的权值,输出哪个叶子使它到根的路径上权和最小。

思路:

输入后建树,然后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 建树的更多相关文章

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

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

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

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

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

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

  4. 【紫书】Tree UVA - 548 静态建树dfs

    题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...

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

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

  6. Uva 548 Tree

    0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("bui ...

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

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

  8. UVA - 548 Tree(二叉树的递归遍历)

    题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...

  9. UVa 548 Tree(中序遍历+后序遍历)

    给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...

随机推荐

  1. Entity Framework入门教程(6)--- 在线场景中保存数据

    在线场景中保存数据 在线场景中保存实体数据是一项相当容易的任务,因为使用的是同一个context,这个context会自动跟踪所有实体发生的更改. 下图说明了在线场景中的CUD(创建,更新,删除)操作 ...

  2. Quartz.net 3.x使用总结(一)——入门介绍

    1.Quartz.net简介 Quartz.NET是一个强大.开源.轻量级的任务调度框架.任务调度在我们的开发中经常遇到,如说:每天晚上三点让程序或网站执行某些代码,或者每隔5秒种执行一个方法等.Wi ...

  3. JS的基础知识回顾

    前言:JS应用在浏览器端,基于浏览器事件执行,功能十分强大,不容错过

  4. ACM-ICPC 2018 徐州赛区网络预赛 J Maze Designer(最大生成树+LCA)

    https://nanti.jisuanke.com/t/31462 题意 一个N*M的矩形,每个格点到其邻近点的边有其权值,需要构建出一个迷宫,使得构建迷宫的边权之和最小,之后Q次查询,每次给出两点 ...

  5. MyEclipse 2015 Stable 2.0破解方法

    本篇博文简单介绍一下利用网上说明的方法破解MyEclipse 2015 Stable 2.0的具体细节.因为原来在贴吧上的方法不够详细,所以本人重新整理了一下.方法源自:http://tieba.ba ...

  6. postfix - SPF 防发件人欺骗

    安装 perl 依赖: yum install perl-Mail-SPF perl-Sys-Hostname-Long 下载 SPF 插件工具: wget https://launchpad.net ...

  7. LeetCode前100题(EASY难度)

    1 Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  8. 20165230 Exp3 免杀原理与实践

    目录 1.实验内容 2.基础问题回答 3.实验内容 任务一:正确使用免杀工具或技巧 使用msf编码器,msfvenom生成如jar之类的其他文件 使用veil-evasion 自己利用shellcod ...

  9. LOJ 3049: 洛谷 P5284: 「十二省联考 2019」字符串问题

    题目传送门:LOJ #3049. 题意简述: 给定一个长度为 \(n\) 的母串 \(S\). 有 \(n_a\) 个 A 类串,都是 \(S\) 的子串,以区间的形式给出. 有 \(n_b\) 个 ...

  10. PHP 【四】

    数组 $string = array(x,y,z); <?php$cars=array("Volvo","BMW","Toyota") ...