题意:

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

思路:

输入后建树,然后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. Activation HDU - 4089(概率dp)

    After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy ...

  2. CF809E Surprise me!(莫比乌斯反演+Dp(乱搞?))

    题目大意: 给你一棵树,树上的点编号为\(1-n\).选两个点\(i.j\),能得到的得分是\(\phi(a_i*a_j)*dis(i,j)\),其中\(dis(i,j)\)表示\(a\)到\(b\) ...

  3. Linux-逻辑卷LVM

    LVM逻辑卷管理器 为什么要使用逻辑卷? 逻辑卷管理器是Linux系统用于对硬盘分区进行管理的一种机制,为了解决硬盘设备在创建分区后不易修改分区大小的缺陷.尽管对传统的硬盘分区进行强制扩容或缩容从理论 ...

  4. crm 添加用户 编辑用户 公户和私户的展示,公户和私户的转化

    1.添加用户 和编辑可以写在一起 urls.py url(r'^customer_add/', customer.customer_change, name='customer_add'), url( ...

  5. sprin源码解析之属性编辑器propertyEditor

    目录 异常信息 造成此异常的原因 bean 配置文件 调用代码 特别说明: 异常解决 注册springt自带的属性编辑器 CustomDateEditor 控制台输出 属性编辑器是何时并如何被注册到s ...

  6. ArcGis安装失败提示“需要Microsoft .NET Framework 3.5 sp1或等效环境”的解决方法

    这个问题一般出现在Win8或者Win10系统上,因为系统默认没有启用该.Net Framework. 下载Microsoft .NET Framework 3.5 sp1安装后再开始安装ArcGis. ...

  7. [物理学与PDEs]第5章第4节 本构方程 - 应力与变形之间的关系

    5. 4 本构方程 - 应力与变形之间的关系 5.4.1. 本构关系的一般形式 1. 若 Cauchy 应力张量 ${\bf T}$ 满足 $$\bex {\bf T}({\bf y})=\hat{\ ...

  8. [物理学与PDEs]第4章习题4 一维理想反应流体力学方程组的守恒律形式及其 R.H. 条件

    写出在忽略粘性与热传导性, 即设 $\mu=\mu'=\kappa=0$ 的情况, 在 Euler 坐标系下具守恒律形式的一维反应流动力学方程组. 由此求出在解的强间断线上应满足的 R.H. 条件 ( ...

  9. java8 按条件过滤集合

    //黄色部分为过滤条件list.stream().filter(user-> user.getId() > 5 && "1组".equals(user. ...

  10. cmd 命令添加防火墙端口

    windows dos 命令添加防火墙端口. 示例 123 端口: netsh firewall add portopening protocol = UDP port = name = NTPSER ...