UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小。
分析:已知中序后序建树,再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(二叉树的递归遍历)的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVA 548(二叉树重建与遍历)
J - Tree Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Ap ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVa 548 (二叉树的递归遍历) Tree
题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 二叉树的递归遍历 The Falling Leaves UVa 699
题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与 ...
- 二叉树的递归遍历 Tree UVa548
题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...
- 数据结构之二叉树篇卷三 -- 二叉树非递归遍历(With Java)
Nonrecursive Traversal of Binary Tree First I wanna talk about why we should <code>Stack</c ...
- C++编程练习(17)----“二叉树非递归遍历的实现“
二叉树的非递归遍历 最近看书上说道要掌握二叉树遍历的6种编写方式,之前只用递归方式编写过,这次就用非递归方式编写试一试. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...
随机推荐
- Codeforces Round #580 (Div. 2)D(思维,Floyd暴力最小环)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;const int maxn=300;cons ...
- 「Luogu P1383 高级打字机」
一道非常基础的可持久化数据结构题. 前置芝士 可持久化线段树:实现的方法主要是主席树. 具体做法 这个基本就是一个模板题了,记录一下每一个版本的字符串的长度,在修改的时候就只要在上一个版本后面加上一个 ...
- [zlib] CMake + VS2015 下编译zlib,及使用小例
下载与解压 可以到zlib官网 http://zlib.net/ 下载,这个没有被墙的,如果下载,试一下换个网络再访问.因为我在公司的网络是访问不了的. 我当前使用的是 zlib1.2.11版本. 下 ...
- 「NOIP2017」逛公园
传送门 Luogu 解题思路 考虑 \(\text{DP}\). 设 \(f[u][k]\) 表示从 \(u\) 到 \(n\) 走过不超过 \(Mindis(u, n) + k\) 距离的方案数. ...
- Shenandoah 与 ZGC
简介 Shenandoah GC 与 ZGC 同为新一代的低延迟收集器, 分别由RedHat和Oracle开发, 目前还在实验阶段, 尚未使用于生产环境. GC的三项指标: Footprint(内存占 ...
- 【剑指Offer面试编程题】题目1516:调整数组顺序使奇数位于偶数前面--九度OJ
题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 输入: 每个输 ...
- Percona XtraBackup不锁库搭建slave数据库-基于GTID
Percona XtraBackup不锁库搭建slave数据库-基于GTID 1.下载安装epel源并安装 wget http://ftp.cuhk.edu.hk/pub/linux/fedora-e ...
- 回顾PHP:第一章:PHP基础语法
第一章:PHP基础语法 一.常量: 1.1.define()函数:define(‘R’,’5’,’true’); //R:常量名,5:常量值,true:常量名对大小写不敏感,为false时表示对常量名 ...
- android使用友盟实现第三方登录、分享以及微信回调无反应问题解决办法
这里介绍微信和新浪登录.微信登录和新浪登录都需要申请第三方账号.可以参考官方文档http://dev.umeng.com/social/android/operation#2还是很清晰的. 新浪微博开 ...
- Metasploit学习笔记——强大的Meterpreter
1. Meterpreter命令详解 1.1基本命令 使用Adobe阅读器渗透攻击实战案例打开的Meterpreter会话实验,靶机是WinXP.由于所有命令与书中显示一致,截图将书中命令记录下来. ...