题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小。

分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, };
const int dc[] = {-, , , };
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
string a, b;
vector<int> in_order, post_order;
int leftchild[MAXN];
int rightchild[MAXN];
int anssum;
int ansv;
int build_tree(int L1, int R1, int L2, int R2){
if(L1 > R1) return ;
int root = post_order[R2];
int st = L1;
while(in_order[st] != root) ++st;
int cnt = st - L1;//一定要通过个数来控制取出来的中序后序序列的左右下标
leftchild[root] = build_tree(L1, st - , L2, L2 + cnt - );//值为root的左孩子结点的值,第四个参数不能写成st-1,因为取出来的相对应的中序和后序序列不一定是下标对齐的
rightchild[root] = build_tree(st + , R1, L2 + cnt, R2 - );
return root;
}
void dfs(int root, int sum){
sum += root;
if(!leftchild[root] && !rightchild[root]){//叶子
if(sum < anssum || (sum == anssum && root < ansv)){
anssum = sum;
ansv = root;
}
}
if(leftchild[root]){
dfs(leftchild[root], sum);
}
if(rightchild[root]){
dfs(rightchild[root], sum);
}
}
int main(){
while(getline(cin, a)){
in_order.clear();
post_order.clear();
memset(leftchild, , sizeof leftchild);
memset(rightchild, , sizeof rightchild);
stringstream s1(a);
int x;
while(s1 >> x){
in_order.push_back(x);
}
getline(cin, b);
stringstream s2(b);
while(s2 >> x){
post_order.push_back(x);
}
int len = in_order.size();
build_tree(, len - , , len - );
int root = post_order[len - ];
anssum = INT_M_INF;
ansv = INT_M_INF;
dfs(root, );
printf("%d\n", ansv);
}
}

已知中序和后序可建树,建成后,可输出前序序列。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, };
const int dc[] = {-, , , };
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
string a, b;
vector<int> in_order, post_order, pre_order;
int leftchild[MAXN];
int rightchild[MAXN];
int build_tree(int L1, int R1, int L2, int R2){
if(L1 > R1) return ;
int root = post_order[R2];
int st = L1;
while(in_order[st] != root) ++st;
int cnt = st - L1;//一定要通过个数来控制取出来的中序后序序列的左右下标
leftchild[root] = build_tree(L1, st - , L2, L2 + cnt - );//值为root的左孩子结点的值,第四个参数不能写成st-1,因为取出来的相对应的中序和后序序列不一定是下标对齐的
rightchild[root] = build_tree(st + , R1, L2 + cnt, R2 - );
return root;
}
void dfs(int root){
pre_order.push_back(root);
if(leftchild[root]){
dfs(leftchild[root]);
}
if(rightchild[root]){
dfs(rightchild[root]);
}
}
int main(){
while(getline(cin, a)){
in_order.clear();
post_order.clear();
pre_order.clear();
memset(leftchild, , sizeof leftchild);
memset(rightchild, , sizeof rightchild);
stringstream s1(a);
int x;
while(s1 >> x){
in_order.push_back(x);
}
getline(cin, b);
stringstream s2(b);
while(s2 >> x){
post_order.push_back(x);
}
int len = in_order.size();
build_tree(, len - , , len - );
int root = post_order[len - ];
dfs(root);
for(int i = ; i < len; ++i){
if(i) printf(" ");
printf("%d", pre_order[i]);
}
printf("\n");
}
}

UVA - 548 Tree(二叉树的递归遍历)的更多相关文章

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

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

  2. UVA 548(二叉树重建与遍历)

    J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Ap ...

  3. UVa 548 Tree(中序遍历+后序遍历)

    给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...

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

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

  5. UVa 548 (二叉树的递归遍历) Tree

    题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...

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

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

  7. 二叉树的递归遍历 The Falling Leaves UVa 699

    题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与 ...

  8. 二叉树的递归遍历 Tree UVa548

    题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...

  9. 数据结构之二叉树篇卷三 -- 二叉树非递归遍历(With Java)

    Nonrecursive Traversal of Binary Tree First I wanna talk about why we should <code>Stack</c ...

  10. C++编程练习(17)----“二叉树非递归遍历的实现“

    二叉树的非递归遍历 最近看书上说道要掌握二叉树遍历的6种编写方式,之前只用递归方式编写过,这次就用非递归方式编写试一试. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...

随机推荐

  1. idea右键新建选项没有类和包的创建方式

    Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure

  2. 7(计算机网络) ICMP与ping

    无论是在宿舍,还是在办公室,或者运维一个数据中心,我们常常会遇到网络不通的问题.那台机器明明就在那里,你甚至都可以通过机器的终端连上去看.它看着好好的,可是就是连不上去,究竟是哪里出了问题呢? ICM ...

  3. redis集群在线迁移

    地址规划 主机名 ip地 端口 redis01 10.0.0.10 6379.6380 redis02 10.0.0.60 6379.6380 redis03 10.0.0.61 6379.6380 ...

  4. 2.分析Ajax请求并抓取今日头条街拍美图

    import requests from urllib.parse import urlencode # 引入异常类 from requests.exceptions import RequestEx ...

  5. STP 指定端口 根端口 区别和理解

    不多说,先上图,A为指定端口,B为非指定端口. 看本文的网友应该知道根端口和指定端口的选举,但是对指定端口和根端口的理解不清楚.这里我就略过选举过程,直接描述这两者的区别和存在的意义. 指定端口:转发 ...

  6. 超级方便的linux命令手册

    超级方便的linux命令手册 NO 分类 PS1 命令名 用法及参数 功能注解 对应章节 1 文件管理 # ls ls -a 列出当前目录下的所有文件,包括以.头的隐含文件     文件管理 # ls ...

  7. mathematica练习程序(第一章 Mathematica的基本量)

    虽然过去有用Mathematica解过一些问题,不过对这个语言并没有系统学习过. 所以最近想重新把Mathematica系统的学一遍. 偶然在B站上找到了这样一组教程:https://www.bili ...

  8. Restful API及接口安全

    一.简介 REST(Representational State Transfer,具体状态转移),是一种基于HTTP协议.URI(统一资源定位符).JSON和XML这些现有协议与标准的,针对网络应用 ...

  9. PAT B1020 月饼

    #include<iostream> #include<algorithm> using namespace std; struct mooncake { double sto ...

  10. (4)LoraWAN:Physical Message Formats

    四.Physical Message Formats LoRa数据包结构 LoRaTM调制解调器采用隐式和显式两种数据包格式.其中,显式数据包的报头较短,主要 包含字节数.编码率及是否在数据包中使用循 ...