点击打开链接
题目意思:给定一颗二叉树后序和中序,求出这颗二叉树从根节点到叶子点的和的最小值,输出最小值的叶子节点的值
解题思路:1利用模板建立一颗二叉树 2 遍历求最小的和的叶子节点的值
代码:
#include <cctype>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int MAXN = 100010;
//结构体的结构
struct Btree{
int val;
struct Btree *lchild;
struct Btree *rchild;
struct Btree *father;//定义一个父亲指针
};
Btree *root , *temp;
int post[MAXN] , mid[MAXN];
int len , nodesum[MAXN] , node[MAXN];//用来存储最小的叶子节点的值
int pathnum[MAXN] , Minsum , Minnode , sum;
//初始化二叉树
void newBtree(Btree *u){
u -> lchild = NULL;
u -> rchild = NULL;
u -> father = NULL;
}
//建树(使用模板见我的博客)
Btree* InPostCreateTree(int *mid , int *post , int len){
if(len == 0)
return NULL;
int i = len-1;
while(post[len-1] != mid[i])
--i;
Btree *root = new Btree;
newBtree(root);
root -> val = post[len-1];
root -> lchild = InPostCreateTree(mid,post,i);
root -> rchild = InPostCreateTree(mid+i+1,post+i,len-i-1);
return root;
}
//输入函数(先输入中序)
int input(){
char space;
int i , j , n , len , count = 0;
i = 1;j = 0;
while(scanf("%d%c" , &n , &space)){
if(count == 0){
mid[i] = n;
i++;
}
if(count == 1){
post[j] = n;
j++;
}
if(space == '\n')
count++;
if(count == 2)
break;
}
return i;
}
//求解最小路径值的叶子节点
//我们用一个index来表示当前的位置,用tempsum数组存储读入的值,只要到了叶子节点就计算上面一次的和,更新Minsum 和 Minnode
void minpath(Btree *root , int Index){
if(root==NULL) return ; //注意加上这一句
if(root -> lchild == NULL && root -> rchild == NULL){//如果为叶子节点
pathnum[Index] = root -> val;//这句重点
for(int i = 0 ; i <= Index ; i++)
sum += pathnum[i];//求到该叶子节点的和
if(Minsum > sum){//是否更新
Minsum = sum;
Minnode = pathnum[Index];
}
sum = 0;//注意每一计算完为0
--Index;//向上一步
return;
}
if(root != NULL){
pathnum[Index] = root->val;//每次把值存入数组
minpath(root->lchild , Index+1);//注意参数的传入
minpath(root->rchild , Index+1);
}
}
//处理问题函数
void solve(){
root = new Btree;
temp = new Btree;
newBtree(root);//初始化,注意new出来后要初始化
newBtree(temp);
root = InPostCreateTree(mid , post , len);
Minsum = 999999999;//开始为大数
sum = 0;
memset(pathnum , 0 , sizeof(pathnum));
minpath(root , 0);
cout<<Minnode<<endl;
}
//主函数
int main(){
//freopen("input.txt" , "r" , stdin);
int n;
char ch;
while(~scanf("%d%c" , &n , &ch)){
mid[0] = n;
if(ch == '\n'){
scanf("%d" , &post[0]);
cout<<mid[0]<<endl;
continue;
}
else
len = input();
solve();
}
return 0;
}
分享到:
相关推荐
题目:https://vjudge.net/problem/UVA-548 #include #include #include #include using namespace std; const int manx = 10000 + 5; int in_order[manx], post_order[manx], lch[manx], rch[manx]; int n; bool ...
uva272
UVA109的题解,经测试完全正确,还附有题解。
有uva刘汝佳文件夹的50道题解,从数据结构开始,以后慢慢上传
包含UVA在线OJ系统的绝大部分的示例代码,并都已AC,可在刷题时参考
UVa在我看来是比较全的一个题解,希望能帮助大家。欢迎下载。
uva最全ac代码
uva531最长公共子序列问题水题,应用简单的dp即可ac有更快速的方法欢迎讨论
uva10755 ac 代码,可以随意更改下载
uva357的栈实现版本
UVA 题目,不是很难,试试吧
1.Uva_base的编译 在编译球队时,则需要在当前球队文件夹下打开终端输入执行以下命令(以下命令都是在root下执行的): ./configure make clean make 如果运行Uva_base后,出现球员越界或掉线的情况,就重新...
《算法竞赛入门经典》UVa配套题目pdf版完整
世界著名大学UVA OJ平台上的题目部分分类,分的不好请原谅。
这是一支完整的uva球队,包含所有基本模块,初者可在上修改得到自己的球队
uva_trilearn2002 源代码
主要是uvaoj习题相关题目 练习题目
这里面全部为在Uva Online Judge上面的部分题目的解答,里面提供了解答使用的源代码。
开源项目-codingsince1985-UVa.zip,Been solving UVa Online Judge Problems in Golang for one year (and counting)
PDF试题