UVA 548 Tree 中序+后序构造二叉树dfs

本文介绍了一道关于二叉树的问题,给定中序和后序遍历序列,求解从根节点到叶子节点的路径中权值之和最小的路径及其终点叶子节点的权值。通过递归构建二叉树并使用深度优先搜索算法解决该问题。

Tree

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.
Input
The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.
Output
For each tree description you should output the value of the leaf node of a path of least value. In the
case of multiple paths of least value you should pick the one with the least value on the terminal node.
Sample Input
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
Sample Output
1
3
255

题目链接:https://vjudge.net/problem/UVA-548

题意

给出二叉树的中序遍历序列和后序遍历序列,以及每个节点的权值,求从叶子节点到根节点权值和最小且最小权值的叶子的权值。

题解

后序遍历序列的最后一个点便是根节点,在中序序列中找到根节点,便可以得到根节点左右子节点的个数,递归求解,可以构造出二叉树。dfs计算从根节点到叶子节点的权值和,求出最小值。
读取一行:getline(cin, line),stringstream是字符串流。

//#include <bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<sstream>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const int maxn = 10010;
int lf[maxn], rt[maxn], post_order[maxn], in_order[maxn];
int n, best, best_sum;

bool input(int *a){
    n = 0;
    string line;
    if(!getline(cin, line)) return false;
    stringstream ss(line);
    int x;
    while(ss>>x) a[n++] = x;
    return n>0;
}

int build(int l1, int r1, int l2, int r2){
    if(l1>r1) return 0;
    int root = post_order[r2];
    int p = l1;
    while(in_order[p] != root) p++;
    int cnt = p-l1;
    lf[root] = build(l1, p-1, l2, l2+cnt-1);
    rt[root] = build(p+1, r1, l2+cnt, r2-1);
    return root;    //易漏 
}

void dfs(int u, int sum){
    sum += u;
    if(!lf[u] && !rt[u]){
        if(sum<best_sum || sum == best_sum && u<best){
            best = u, best_sum = sum;
        }
    }
    if(lf[u]) dfs(lf[u], sum);
    if(rt[u]) dfs(rt[u], sum);
}

int main(){
    while(input(in_order)){
        input(post_order);
        build(0, n-1, 0, n-1);
        best = INF, best_sum = INF;
        dfs(post_order[n-1], 0);
        printf("%d\n", best);
    }
    return 0;
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值