励志用尽量少的代码做高效的表达。


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


题目大意

给定二叉树的中序和后序遍历序列,求该二叉树中根到叶子的路径和最小者,若存在多个解,则选择叶子权值最小者(叶子权值不重复)。

思路分析

此题本质是中后序建树+求路径最小权。
(PS:书上的数组建树用不习惯,于是自己写了一个指针建树。)
关键在于中序和后序建树,后序遍历序列的最后一个元素来确定根(前序的话是第一个元素确定根),中序序列来划分左右子树,如此递归建立树
对于路径和求解,建树完成后对树进行dfs

#include<bits/stdc++.h>
using namespace std;
struct Node {int v;                         //值Node *l = NULL, *r = NULL;     //左右子树
};
vector<int> in, post;             //中序,后序存储
int minsum = 0x3fffff, ans = -1;  //路径最小和、答案(叶子节点)
Node* createTree(int i1, int j1, int i2, int j2) {if(i1 >= j1 || i2 >= j2) return NULL;     //空树Node* root = new Node;root->v = post[j2-1];            //根节点为后续遍历最后一个数int j = find(in.begin()+i1, in.begin()+j1, post[j2-1]) - in.begin();//在中序遍历中查找该值root->l = createTree(i1, j, i2, i2+(j-i1));      //建立左子树root->r = createTree(j+1, j1, i2+(j-i1), j2-1);    //建立右子树return root;
}
void dfs(Node* root, int sum) {     //计算到每个叶子的路径和并记录最小者if(root->l == NULL && root->r == NULL) {       //判断是否遍历到头 sum += root->v;if(sum < minsum || (sum == minsum && ans > root->v)) {minsum = sum;ans = root->v; } return;}if(root->l != NULL) dfs(root->l, root->v+sum);    //非空,则访问左子树if(root->r != NULL) dfs(root->r, root->v+sum); //非空,则访问右子树
}
int main() {string s1, s2;int s;while(getline(cin, s1) && getline(cin, s2)) {in.clear(); post.clear();                  //初始化stringstream input1(s1), input2(s2);while(input1 >> s) in.push_back(s);while(input2 >> s) post.push_back(s);Node* btree = createTree(0, in.size(), 0, post.size());   //建树minsum = 0x3fffff; dfs(btree, 0);          //遍历计算printf("%d\n", ans); }return 0;
}

收获:

1、巩固了DFS
2、掌握了中后序遍历建树。
3、掌握了求最小权路径的方法。


择苦而安,择做而乐,虚拟现实终究比不过真实精彩之万一。

43行代码AC——例题6-8 树(Tree,UVa 548)——解题报告相关推荐

  1. 21行代码AC——例题5-2 Ducci序列(Ducci Sequence,UVa1594)——解题报告

    励志用少的代码做高效的表达. 题目(提交)链接→UVa-1594 本题为水题,因此侧重点从解题转变为优化. 注意点: 1.下一轮是按照上一轮的每个数做运算,但下一轮每次运算都会改变数列的值,造成运算不 ...

  2. 15行代码AC——习题5-5 复合词(Compound Words, UVa 10391)——解题报告

    励志用少的代码做高效的表达 题目(提交)链接→UVA-10391 本题实质是#include<string>头文件的substr()字符串分割函数与#include<algorith ...

  3. 17行代码AC——L1-030 一帮一 (15分)(解题报告)

    立志用更少的代码做更高效的表达 "一帮一学习小组"是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组.本题就请你编写程序帮助老师自动完成这个分配工作 ...

  4. 9行代码AC——L1-022 奇偶分家(10 分)(~解题报告~)

    立志用更少的代码做更高效的表达 给定N个正整数,请统计奇数和偶数各有多少个? 输入格式: 输入第一行给出一个正整N(≤1000):第2行给出N个非负整数,以空格分隔. 输出格式: 在一行中先后输出奇数 ...

  5. 10行代码AC——L1-018 大笨钟 (10分)(~解题报告~)

    立志用更少的代码做更优化的表达 微博上有个自称"大笨钟V"的家伙,每天敲钟催促码农们爱惜身体早点睡觉.不过由于笨钟自己作息也不是很规律,所以敲钟并不定时.一般敲钟的点数是根据敲钟时 ...

  6. 38行代码AC——L1-025 正整数A+B (15分)(~解题报告~)

    立志用更少的代码做更高效的表达 题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空 ...

  7. 43行代码AC——HDU 1757 A Simple Math Problem(矩阵快速幂,附快速幂讲解)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 代码(去掉空行43行) #include<iostream> #inclu ...

  8. 16行代码AC——例题6-4破损的键盘(Broken Keyboard,UVa 11988)——解题报告

    励志用尽量少的代码做高效的表达. 题目(提交)链接→UVa-11988 题目大意: 输入一个字符串,输出在原本应该是怎么样的? 具体方法是: 若读取到'[', 则执行Home键:将光标移到行首. 若读 ...

  9. 15行代码AC_ 【蓝桥杯】兴趣小组(解题报告+思考)

    励志用更少的代码做更高效的表达 问题描述: 为丰富同学们的业余文化生活,某高校学生会创办了3个兴趣小组 (以下称A组,B组,C组). 每个小组的学生名单分别在[A.txt],[B.txt]和[C.tx ...

最新文章

  1. JVM的垃圾回收机制详解和调优
  2. 云计算学习(3-3)云计算的由来-应运而生
  3. js判断一个对象是否为空
  4. xcode 左侧导航栏 no finder results 问题的解决方法
  5. oracle rman恢复表空间,Oracle数据库RMAN恢复之表空间和数据块介质的恢复
  6. Spring Boot 学习(一) 快速搭建SpringBoot 项目
  7. 如何向女朋友解释什么是HTTP协议
  8. java中IO写文件工具类
  9. 浅谈语音识别技术的发展趋势与应用前景 - 全文
  10. Java面试易错题精选
  11. ETL过程中数据清洗(脏数据处理)小结
  12. Java11完全兼容Java8吗_Java11正式发布,要不要升级请看这里!
  13. 南方h5手簿求转换参数_如何使用RTK手簿求坐标转换参数(四参数/七参数)
  14. 让别人关机!(VB语言)
  15. 安卓手机并没有“校准电池”的方式
  16. Vue -- vue-router(路由)的基本使用
  17. 基于安卓系统的SM4-SM2/3加解密软件开发报告
  18. sudo su后获取不到JAVA_HOME环境变量的解决方案
  19. Linux glib库hash表GHashTable介绍
  20. 模拟JS触发按钮点击功能

热门文章

  1. scrapy发送翻页请求
  2. 深度好文:Netflix奈飞微服务架构设计解析
  3. OS- -内存之地址空间
  4. 走近5G云游戏标准制定——握住互联网世界看不见的手
  5. 实时音视频流媒体传输的思考和实践
  6. 数据结构与算法之KMP算法
  7. CSS干货直击:腾讯无边界访问控制体系建设
  8. 互动直播的技术细节和解决方案实践经验谈
  9. 从nginx-rtmp中提取一帧h264帧
  10. RTMPdump(libRTMP) 源代码分析 7: 建立一个流媒体连接 (NetStream部分 2)