题意:

根据二叉树中序和后序建立二叉树,从根结点开始计算和到叶子结点,输出总和最小的叶子结点,如果有俩个和一样大,输出叶子结点最小的

AC:80ms

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<sstream>
using namespace std;
struct Tree
{
int curTotal;
Tree()
{
curTotal = 0;
}
};
void buildTree(const int* in, const int* post, int curTotal, int* minTotal,
int tl, int * leaf);
int readInt(string str, int * const a)
{
istringstream in(str);
int i;
int total = 0;
while (in >> i)
a[total++] = i;
return total;
} int main()
{
//freopen("d:\\1.txt", "r", stdin);
string str1, str2;
while (getline(cin, str1))
{
getline(cin, str2);
int inorder[100000];
int postorder[100000];
int tl = readInt(str1, inorder);
readInt(str2, postorder);
int min = 0x7FFFFFFF;
int leaf = 0x7FFFFFFF;
buildTree(inorder, postorder, 0, &min, tl, &leaf);
cout << leaf << endl;
}
}
void buildTree(const int* in, const int* post, int curTotal, int* minTotal,
int tl, int * leaf)
{
if (tl == 0)
return;
int cur = -1;
for (int i = 0; i < tl; i++)
{
if (in[i] == post[tl - 1])
{
cur = i;
break;
}
}
//左孩子长度cur
int left = cur;
//right长度 tl-cur-1
int right = tl - cur - 1;
curTotal += in[cur];
if (tl == 1)
{
if (curTotal <= *minTotal)
{
*minTotal = curTotal;
*leaf = in[cur];
}
} //left
buildTree(in, post, curTotal, minTotal, left, leaf);
//right
buildTree(in + left + 1, post + left, curTotal, minTotal, right, leaf);
}

  

UVA548的更多相关文章

  1. 【日常学习】【二叉树遍历】Uva548 - Tree题解

    这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...

  2. 例题6-8 Tree Uva548

    这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该 ...

  3. UVA548——Tree(中后序建树+DFS)

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

  4. 二叉树的递归遍历 Tree UVa548

    题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...

  5. UVA548 tree的思路

    唔,首先这题给出了中序遍历和后序遍历要求我们求出, 一个叶子节点到根的数值总和最小,且这个叶子节点是最小的那个 这题的难点在于如何运用中序遍历和后序遍历还原整棵树, 这里有两个方法: 1. 递归构造原 ...

  6. 6-8 树 uva548

    read 的方式值得学习 当不知道每一行有多少个输入的时候 getline  在弄成stringstream!一个一个处理 用built递归的方式化大为小进行建立树 dfs 遍历整个树来求最值 变量的 ...

  7. 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】

    [已知先序.中序求后序排列]--字符串类型 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和 ...

  8. UVA548 Tree (二叉树的遍历)

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

  9. UVA548(二叉树遍历)

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

随机推荐

  1. ReSharper2017.3的列对齐、排版格式、列对齐错误的修复

    ReSharper代码排版格式 列对齐 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  2. 内网渗透中SSh的巧用

    后续应该会做个实例 转自:http://www.myhack58.com/Article/html/3/8/2009/25156.htm 经常遇到如下情形,内部网络主机通过路由器或者安全设备做了访问控 ...

  3. restheart 基本使用

    restheart 是一个方便基于mongodb的restapi 开发框架 参考项目 https://github.com/rongfengliang/restheart-docker-compose ...

  4. nodejs 使用express开发获取其他网站引用本站点js文件的参数

    nodejs进行站点的开发其性能是很好的,在js 大行其道的天下,使用js基本上可以干好多只要我们能想到的东西,我们可以使用js文件进行用户验证等等. 这次我们就使用express 进行获取其他站点引 ...

  5. 命令行net time同步时间(内网)

    首先还是推荐大家使用Internet时间来同步自己计算机的时间,这样做主要是方便,就是设置一个ntp服务器,我推荐下面的三个ntp服务器地址. time.asia.apple.com //亲测有效 a ...

  6. div+css 怎么让一个小div在另一个大div里面 垂直居中

    div+css 怎么让一个小div在另一个大div里面 垂直居中 方法1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 .parent {           width:800 ...

  7. RAC5——11gR2以后GI进程的变化

    参考文档: 11gR2 Clusterware and Grid Home - What You Need to Know (Doc ID 1053147.1)诊断 Grid Infrastructu ...

  8. spring4 知识点

    1 bean的 创建 1,直接在配置文件里面写一个带有@Bean注解的方法(返回值就是那个bena对象),(name等于 方法名) 2,使用 FactoryBean 接口(三个方法分别是创建,类型,单 ...

  9. NOI2002银河英雄传说——带权并查集

    题目:https://www.luogu.org/problemnew/show/P1196 关键点在于存下每个点的位置. 自己糊涂的地方:位置是相对于谁的位置? 因为每次给一个原来是fa的点赋位置时 ...

  10. Microsoft Dynamics CRM 2011 面向Internet部署 (IFD) CRM 登录出现会话超时的解决办法

    一.IFD 登录的时候,过了一段时间,会马上出现“您的会话已过期”,怎么解决这个问题呢,可以通过改变这个时间.具体图如二 Link to Dynamics CRM Wiki Home Page 二.S ...