题意:

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

思路:

输入后建树,然后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. [Coci2015]Divljak

    题目描述  Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

  2. 如何确定Kafka的分区数、key和consumer线程数

    [原创]如何确定Kafka的分区数.key和consumer线程数   在Kafak中国社区的qq群中,这个问题被提及的比例是相当高的,这也是Kafka用户最常碰到的问题之一.本文结合Kafka源码试 ...

  3. 联想的笔记本有隐藏分区 导致无法安装win10 eufi启动 报错:windows无法更新计算机的启动配置。无法安装

    联想的笔记本都带着类似一键还原等的系统恢复软件,这些软件往往是将出厂设置备份在单 独的一个分区,此分区默认为隐藏,在 Windows 的磁盘管理中可以看到.打开磁盘管理器 的方法是右击计算机——管理, ...

  4. class文件解释

  5. Windows 环境下的 protoc 安装(转)

    如果是为了编译hadoop2.8.0源码,必须使用2.5.0版本的protobuf,安装方法同下 1. 下载需要的安装包:https://github.com/google/protobuf/rele ...

  6. JN_0003:JS定义变量的3种方式

    js中三种定义变量的方式const, var, let的区别. 1,const定义的变量不可以修改,而且必须初始化. 2,var定义的变量可以修改,如果不初始化会输出undefined,不会报错. 3 ...

  7. [数学杂志]AML

    Copied from: http://www.elsevier.com/journals/applied-mathematics-letters/0893-9659/guide-for-author ...

  8. [物理学与PDEs]第1章习题12 Coulomb 规范下电磁场的标势、矢势满足的方程

    试给出在 Coulomb 规范下, 电磁场的标势 $\phi$ 与矢势 ${\bf A}$ 所满足的方程. 解答: 真空中的 Maxwell 方程组为 $$\bee\label{1_10_12:eq} ...

  9. JAVA进阶13

    间歇性混吃等死,持续性踌躇满志系列-------------第13天 1.查看线程的运行状态 package code0327; class Demo01 implements Runnable { ...

  10. torch画散点图

    import torch from torch.autograd import Variable import torch.nn.functional as F import matplotlib.p ...