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 ...
随机推荐
- Entity Framework Tutorial Basics(5):Create Entity Data Model
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...
- Zoey.Dapper--Dapper扩展之把SQL语句放到文件中
介绍 不知道大家在用Dapper的时候SQL语句是写到哪的,目前看网上的例子都是写到类里面的. 此项目的目的是把SQL语句放到文件(xml)中 目前只是初步版本,只是说明了意图,后面会持续完善和优化 ...
- C#读取文本文件某一行
某一时候,我们只会读取文本文件内某一行.怎样读?还是用for或foreach循环?其实操作起来,很简单,先看看文本文件,如果你也想用下面的文档来做测试,你可以在这个链接进行拷贝:<VB.NET提 ...
- Django之表单form
在登录系统以及需要上传填入的信息时候,用的最多就是表单系统,例如像下面的这种格式 <form action="/form1/" method="post" ...
- 智能合约安全事故回顾(3)-DOS漏洞导致的KotET事件
现实世界中的网络都是有带宽限制的,想象一下,一个访问量稳定的网站,突然有人利用某种方式爆发式的将网站的访问量提升,这个时候系统会作何反应?如果系统没有合理的防DOS攻击的方式,这种时候往往会造成服务器 ...
- sql遍历查询结果sql循环查询结果集sql循环查询
--查询表B,把查询到的数据插入临时表#A中,根据表B 的ID 进行排序:表#A中 的 i 字段 由1开始增加排序: SELECT ROW_NUMBER() OVER ( ORDER ...
- P1527 [国家集训队]矩阵乘法
\(\color{#0066ff}{ 题目描述 }\) 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. \(\color{#0066ff}{输入格式}\) 第一行两个数N,Q ...
- P4213 【模板】杜教筛(Sum)
\(\color{#0066ff}{题 目 描 述}\) 给定一个正整数\(N(N\le2^{31}-1)\) 求 \(\begin{aligned} ans_1=\sum_{i=1}^n\varph ...
- Flume启动报错[ERROR - org.apache.flume.sink.hdfs. Hit max consecutive under-replication rotations (30); will not continue rolling files under this path due to under-replication解决办法(图文详解)
前期博客 Flume自定义拦截器(Interceptors)或自带拦截器时的一些经验技巧总结(图文详解) 问题详情 -- ::, (SinkRunner-PollingRunner-Default ...
- arcgis打印服务
<script> function print1() { require([ "esri/map", ...