Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548

题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根的路径上的权值和最小。如果有多个解,该叶子本身的权值应尽量小。输入中每两行表示一棵树,其中第一行为中序遍历,第二行为后续遍历。
看代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
#include<sstream>
using namespace std;
const int maxv=+;
int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];
int n;
int best,best_sum;//目前为止最优解和对应的权值和
bool read_list(int *a)
{ string line;
if(!getline(cin,line)) return false;//文件结束
stringstream ss(line);
n=;
int x;
while(ss>>x) a[n++]=x;//应用字符串流进行输入
return n>;
}
int build(int L1,int R1,int L2,int R2)
{
if(L1>R1) return ;//空树
int root=post_order[R2];//后序遍历最后一个结点就是树根
int p=L1;
while(in_order[p]!=root) p++;//在中序遍历中找到根结点所在的位置
int cnt1=p-L1;//左子树结点的个数
lch[root]=build(L1,p-,L2,L2+cnt1-);
rch[root]=build(p+,R1,L2+cnt1,R2-);
return root;
}
void dfs(int u,int sum)
{
sum+=u;
if(!lch[u]&&!rch[u])//代表叶子结点
{
if(sum<best_sum||(sum==best_sum&&u<best))
{
best=u;
best_sum=sum;
}
}
if(lch[u]) dfs(lch[u],sum);
if(rch[u]) dfs(rch[u],sum);
}
int main()
{
while(read_list(in_order))
{
read_list(post_order);
build(,n-,,n-);
best_sum=;
dfs(post_order[n-],);
cout<<best<<endl;
}
return ;
}
Tree UVA - 548(二叉树递归遍历)的更多相关文章
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 数据结构之二叉树篇卷二 -- 二叉树递归遍历(With Java)
一.先序递归遍历(Preorder Recursive Traversal) 1.1 算法 首先需要明确的是这里的序是针对 root 节点而言的.故先序即先“访问”根节点,其次“访问”其左右节点. 1 ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- UVa 548 (二叉树的递归遍历) Tree
题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...
- Uva 548 二叉树的递归遍历lrj 白书p155
直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...
- java 二叉树递归遍历算法
//递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRec ...
- python实现二叉树递归遍历与非递归遍历
一.中序遍历 前中后序三种遍历方法对于左右结点的遍历顺序都是一样的(先左后右),唯一不同的就是根节点的出现位置.对于中序遍历来说,根结点的遍历位置在中间. 所以中序遍历的顺序:左中右 1.1 递归实现 ...
- Tree UVA - 548
You are to determine the value of the leaf node in a given binary tree that is the terminal node o ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
随机推荐
- Hive安装及配置
第一步:下载hive并解压 tar zxvf hive-0.8.1-bin.tar.gz 重命名: mv hive-0.8.1-bin hive 给权限:chown hadoop:hadoop hiv ...
- android listview addHeaderView和addFooterView的注意事项
1. item内如果有button等控件时,在监听listview的onitemclick事件时,焦点会被item内的button. imagebutton等控件抢走,从而导致在listview设置了 ...
- CodeForces 703C Chris and Road (简单几何)
题意:有一个n边形的汽车向以速度v向x轴负方向移动,给出零时时其n个点的坐标.并且有一个人在(0,0)点,可以以最大速度u通过w宽的马路,到达(0,w)点.现在要求人不能碰到汽车,人可以自己调节速度. ...
- java求几个数字的和输出详细步骤
设计思想:要求几个数字的和,就要把输入的字符串转换成浮点型,然后求和再输出. 程序流程图: 程序源代码: //此程序用于从命令行接收多个数字,就和并输出. //作者:赵东睿 //2015.9.26 p ...
- boost.asio系列(一)——deadline_timer
一.构造函数 一个deadline_timer只维护一个超时时间,一个deadline_timer不同时维护多个定时器.在构造deadline_timer时指定时间: basic_deadline_t ...
- ElementUI的表单和vee-validate结合使用时发生冲突的解决
在Vue项目中使用ElementUI表单时,同时又引入了vee-validate进行使用的时候,在浏览器上会出现这样的报错: [Vue warn]: The computed property &qu ...
- 【java】小技巧和注意事项
1.字符串反向比较 “abc”.equals(sting) 2.文档注释 /** *注释内容 */ 3.
- docker,mysql,Navicat
Navicat破解网址 https://www.jianshu.com/p/5f693b4c9468 docker pull mysql docker run -d -p 3306:3306 --n ...
- cheerio制作markDown索引目录
原文地址:cheerio制作markDown索引目录 制作目录索引这种东西当然是放在前端方便.选择放在后端一是为了了解Node后端生态,掌握更多后端技术:二是因为公司实行前后端分离的方式开发,睾贵的J ...
- P3750 [六省联考2017]分手是祝愿 期望DP
\(\color{#0066ff}{ 题目描述 }\) Zeit und Raum trennen dich und mich. 时空将你我分开. B 君在玩一个游戏,这个游戏由 \(n\) 个灯和 ...