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)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...
随机推荐
- react - get或set 取值函数
取值函数(getter)和存值函数(setter) 您可以添加以get或set为前缀的方法来创建getter和setter,它们是根据您正在执行的操作执行的两个不同的代码:访问变量或修改其值.对某个属 ...
- SAVE 、BGSAVE和BGREWRITEAOF执行区别
rdbSave 会将数据库数据保存到 RDB 文件,并在保存完成之前阻塞调用者. save 命令直接调用 rdbSave ,阻塞 Redis 主进程:bgsave 用子进程调用 rdbSave ,主进 ...
- 笔记||Python3进阶之读取和写入yaml配置文件
yaml是专门用来写配置文件的语言,简洁强大,远比JSON格式方便,yaml在python语言中有PyYAML安装包. - 首先需要pip安装:pip install pyyaml - yaml基本语 ...
- APNs推送的系统做法
1. #pragma mark - 远程推送注册获得device Token if (IOS_VERSION >= 10.0) { UNUserNotificationCenter * cent ...
- 【快学springboot】在springboot中写单元测试[Happyjava]
前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...
- Mybatis框架模糊查询
一.ISmbmsUserDao层 //根据姓名模糊查询 public List<Smbms> getUser(); //多条件查询 public List<Smbms> get ...
- 【剑指Offer面试编程题】题目1517:链表中倒数第k个结点--九度OJ
题目描述: 输入一个链表,输出该链表中倒数第k个结点. (hint: 请务必使用链表.) 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第一行为两个整数n和k(0< ...
- 格式化JSON插件
参考:https://www.cnblogs.com/whycxb/p/7126116.html
- javaScript中this的指向?
javaScript中this对象是在运行时基于函数的执行环境绑定的,在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象. 但在实际中,代码环境复杂,th ...
- Linux用户和用户组管理命令
一.用户管理命令 1.useradd 创建用户或更新默认新用户的信息 使用方法 useradd [options] 用户名 选项: useradd -u 指定UID具体数值, ...