UVA - 548
Description 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. InputThe 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. OutputFor 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 Input3 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 Output1 3 255 Miguel A. Revilla 1999-01-11
|
[2016-02-08][UVA][548][Tree]
时间: 2024-10-26 03:58:23
[2016-02-08][UVA][548][Tree]的相关文章
UVa 548 Tree(建树,递归遍历)
题意 给你一个树的中序遍历和后序遍历 某个节点的权值为从根节点到该节点所经过节点的和 求权值最小的叶节点的值 如果存在多个 输出值最小的那个 把树建好就好说了 递归递归dfs msun保存最小叶节点权值 ans保存答案 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 10005, INF = 0x3f3f3f3f; int i
UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the terminal node of apath of least value from the root of the binary tree to any leaf. The value of a path is the sum of valuesof nodes along that path.InputT
2016.02.08 值此新年,追梦想
虽然是新年,但已经到了公历的二月份,六分之一已过,离今年的目标有多远?问起自己的目标,一脸茫然,到底目标是什么?梦想又是什么? 每一个季节都需要目标,奋斗的过程需要目标来砥砺自己,丧失目标就是丧失勇气. 梦想不应该在世俗中销声匿迹,相反,梦想应该摆脱世俗的牵绊,作为人生的灯塔. 针对于学习和工作,首要的目标是成为浙江大学优秀的毕业生,让自己的研究生生涯圆满落幕,这是最基本的,也是最需要关注的.对于在蘑菇街的工作和学习,希望自己能够鼓足勇气来学习.不断学习,要知道,只有自己的技术.理论层次高了,才
UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困难啊啊啊啊 后来终于问懂一丢丢了--- 比如说样例: 中序遍历:3 2 1 4 5 7 6 后序遍历:3 1 2 5 6 7 4 首先做第一层: 在后序遍历中的最后一个数为根节点,然后在到中序遍历中找到这个根节点,在这个根节点的左边是左子树,右边是右子树,这样就确定出了左子树和右子树的区间 然后做第
Uva 548 Tree
0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("built:\n"); if(L1>R1) return 0;//空树 int root=post_order[R2]; int p=L1; while(in_order[p] != root) p++; int cnt = p-L1;//左子树的结点个数 lch[root]=build(L1,p-
UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<itera
Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
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
UVA 548(二叉树重建与遍历)
J - Tree Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Appoint description: System Crawler (2014-05-16) Description Tree You are to determine the value of the leaf node in a given binary tree that is the ter
UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: 1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子