UVA - 548 Tree(二叉树的递归遍历)

本文介绍了一种利用中序和后序遍历序列构建二叉树的方法,并进一步探讨了如何通过深度优先搜索(DFS)找到从叶子节点到根节点路径上权值之和最小的路径。此外,还提供了输出前序遍历序列的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

分析:已知中序后序建树,再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[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 10000 + 10;
const int MAXT = 1000000 + 10;
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 0;
    int root = post_order[R2];
    int st = L1;
    while(in_order[st] != root) ++st;
    int cnt = st - L1;//一定要通过个数来控制取出来的中序后序序列的左右下标
    leftchild[root] = build_tree(L1, st - 1, L2, L2 + cnt - 1);//值为root的左孩子结点的值,第四个参数不能写成st-1,因为取出来的相对应的中序和后序序列不一定是下标对齐的
    rightchild[root] = build_tree(st + 1, R1, L2 + cnt, R2 - 1);
    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, 0, sizeof leftchild);
        memset(rightchild, 0, 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(0, len - 1, 0, len - 1);
        int root = post_order[len - 1];
        anssum = INT_M_INF;
        ansv = INT_M_INF;
        dfs(root, 0);
        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[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 10000 + 10;
const int MAXT = 1000000 + 10;
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 0;
    int root = post_order[R2];
    int st = L1;
    while(in_order[st] != root) ++st;
    int cnt = st - L1;//一定要通过个数来控制取出来的中序后序序列的左右下标
    leftchild[root] = build_tree(L1, st - 1, L2, L2 + cnt - 1);//值为root的左孩子结点的值,第四个参数不能写成st-1,因为取出来的相对应的中序和后序序列不一定是下标对齐的
    rightchild[root] = build_tree(st + 1, R1, L2 + cnt, R2 - 1);
    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, 0, sizeof leftchild);
        memset(rightchild, 0, 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(0, len - 1, 0, len - 1);
        int root = post_order[len - 1];
        dfs(root);
        for(int i = 0; i < len; ++i){
            if(i) printf(" ");
            printf("%d", pre_order[i]);
        }
        printf("\n");
    }
}

 

转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/6262183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值