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

分析:已知中序后序建树,再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. ‘.vue’文件(非常重要)

    ‘.vue’文件 Vue自定义了一种后缀名名字为.vue文件,它将html, js, css 整合成一个文件,和里面 template script style三个区别分别依次对应. 1 2 3 4 ...

  2. Android FM模块学习之四源码解析(一)

    转自:http://blog.csdn.net/tfslovexizi/article/details/41516149?utm_source=tuicool&utm_medium=refer ...

  3. C++中的可调用对象

    在C++中,常常会利用函数来简化程序流程.函数的存在使得C++整体更加“模块化”,因而也使得代码可读性大大提高. 在C++程序中,常常会很灵活地调用函数来实现不同的功能与目的(函数重载.继承多态等等) ...

  4. CDC学习

    最近在建立CDC环境,在网上看到一些不错的学习链接,粘贴如下: 1.https://blog.csdn.net/u011729865/article/details/52931366 属于https: ...

  5. mybatis利用generator自动生成的代码

    /** * 排序规则 */ protected String orderByClause; /** * 去重规则 */ protected boolean distinct; /** * where条 ...

  6. Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离。

    Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离.

  7. alert \ confirm \ prompt

    alert() : 会将()中的内容弹出,返回的是()中的内容值,也就是字符串值 confirm :需要用户点击 "确定" 或 "取消" ,若用户点击 ”确定“ ...

  8. pytorch中tensor张量数据基础入门

    pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...

  9. 十五、web中处理乱码问题总结

    一.jsp变成之道---中文乱码 jsp在转换为Servlet的过程经过三次编码转化: 转自  http://www.cnblogs.com/chenssy/p/4235191.html 二.java ...

  10. 0. GC 前置知识

    阅读<垃圾回收的算法与实现>时记录的一些笔记. 对象 在GC的世界中,对象表示的是"通过应用程序利用的数据的集合" 头 我们将对象中保存对象本身信息的部分称为头.头主要 ...