You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

Input

  The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

Output

  For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

Sample Input

3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255

Sample Output

1
3
255

HINT

主要难点就是建树和输出。建树采用的是递归的方式,使用map记录中序数组的下标便于查找,关键的是如何来递归,尤其要注意如何来划分左子树和右子树。具体操作看代码就好。

而输出就是一个查找,直到找到叶子结点,查到最短最小的,然后输出。其他的都是不断累加查找。

Accepted

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<sstream>
#include<fstream>
using namespace std;
vector<int>mid, pos;
map<int, int>id;
int maxx = 0x3fffff,ans=-1; struct TREE{
int num;
TREE* left, * right;
}; TREE* build(int &i,int star,int end) {
if (star>end)return NULL;
TREE* head = new TREE;
head->num = pos[i];
head->left = head->right = NULL;
int p = id[pos[i]]; //找到在中序遍历中的位置
i--;
head->right = build(i, p + 1, end);
if (head->right != NULL)i--; //如果确实插入了就减一
head->left = build(i, star, p - 1);
if (!head->left)i++; //如果没有插入就加一
return head; //返回头指针
} void dfs(TREE* head, int sum) {
if (!head->left && !head->right) {
sum += head->num;
if (sum < maxx || (sum == maxx && ans > head->num)) {
maxx = sum;
ans = head->num;
}
return;
}
if (head->left)dfs(head->left, head->num + sum);
if (head->right)dfs(head->right, head->num + sum);
} int main(){
int t1,t2;
string s,s1,s2;
while (getline(cin, s1)) {
getline(cin, s2);
mid.clear();pos.clear();id.clear();
stringstream ss1(s1), ss2(s2);
while (ss1>>t1&&ss2>>t2){
mid.push_back(t1);
pos.push_back(t2);
id[t1] = mid.size() - 1;
}
int size = pos.size() - 1;
TREE* head = build(size, 0, mid.size() - 1);
maxx = 0x3fffff;
dfs(head, 0);
cout << ans << endl; }
}

Tree UVA - 548的更多相关文章

  1. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

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

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

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

  3. 树(Tree,UVA 548)

    题目描述: 题目思路: 1.使用数组建树 //递归 2.理解后序遍历和中序遍历,建立左右子树 3.dfs深度搜索找出权重最小的路径 #include <iostream> #include ...

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

    题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...

  5. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

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

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

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

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

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

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

  9. Uva 548 Tree

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

随机推荐

  1. Linux+Tomcat+Jdk1.8+jenkins环境搭建

    1.下载jdk的rpm安装包,这里以jdk-8u191-linux-x64.rpm为例进行说明 下载地址:https://www.oracle.com/technetwork/java/javase/ ...

  2. 微信小程序:上滑触底加载下一页

    给商品列表页面添加一个上滑触底加载下一页的效果,滚动条触底之后就发送一个请求,来加载下一页数据, 先在getGoodsList中获取总条数 由于总页数需要再另外的一个方法中使用,所以要把总页数变成一个 ...

  3. SpringCloud(一):微服务架构概述

    1-1.  系统进化理论概述 在系统架构与设计的实践中,经历了两个阶段,一个阶段是早些年常见的集中式系统,一个阶段是近年来流行的分布式系统: 集中式系统: 集中式系统也叫单体应用,就是把所有的程序.功 ...

  4. 后端程序员之路 10、gbdt(Gradient Boosting Decision Tree)

    1.GbdtModelGNode,含fea_idx.val.left.right.missing(指向left或right之一,本身不分配空间)load,从model文件加载模型,xgboost输出的 ...

  5. 关于使用C3P0程序报错Having failed to acquire a resource, com.mchange.v2.resourcepool的问题

    由于是新手的问题,C3P0的使用时严格跟着视频来的,但是问题却来的很突然 在导入了三个包以及创建了路径以后 进行测试 class JdbcutilsTest { @Test void TestGetC ...

  6. c++函数指针说明

    下面随笔说明函数指针用法. 函数指针的定义: 定义形式: 存储类型 数据类型 (*函数指针名)() 含义: 函数指针指向的是程序代码存储区 函数指针的典型用途-----实现函数回调 通过函数指针调用的 ...

  7. jQuery实现游戏推荐

    1.需求:点击添加游戏按钮实现添加游戏,点击删除按钮,删除游戏. 2.实现思路:分别给添加按钮和删除按钮添加click事件. 3.遇到的问题:自己添加的游戏不能进行删除. 4.原因分析:文档加载完毕后 ...

  8. 设计模式之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式的由来 抽象工厂模式,最开始是为了解决操作系统按钮和窗体风格,而产生的一种设计模式.例如:在windows系统中,我们要用windows设定的按钮和窗体,当我们切换Linux系统时,要 ...

  9. 痞子衡嵌入式:盘点国内车规级MCU厂商

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是国内车规级MCU厂商及其产品. 在汽车电子领域,MCU的应用非常广泛,大到车身控制与动力总成,小到雨刷车窗等控制单元,都离不开MCU的身 ...

  10. Django的路由控制

    一.Django中路由到作用 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来 ...