24-3-8二叉树递归遍历

文章描述了一种算法,通过中序和后序遍历构建二叉树,然后利用深度优先搜索找出具有最小路径权重的叶节点,当权重相等时选择编号较小的叶节点。
摘要由CSDN通过智能技术生成

preorder root+left+right    inorder left+root+right    postorder left+right+root

给出中序和后序遍历,找出路径权重值最小的叶节点。最后输出最小路径所对应的叶节点编号。若路径权重值相等,输出叶节点最小的叶节点编号

思路:lch和rch建左右子树,build带中序和后序的左右节点,返回当前树的根节点。root左子树的更新找左子树的根节点,右子树更新同理。因节点用根节点的数组表示,找最小路径用dfs,建立变量sum更新到最后比较

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
const int maxv=10000+10;
int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];
int n;
bool read_list(int* a){
	string line;
	if(!getline(cin,line))return false;
	stringstream ss(line);
	n=0;
	int x;
	while(ss>>x)a[n++]=x;//a挨个保存每列读取的整数
	return n>0;
}
int build(int l1,int r1,int l2,int r2){//前两个数为in_order,后两个数为post_order
	if(l1>r1)return 0;//中序遍历添加节点结束
	int root=post_order[r2];//根节点为后序遍历最后一个
	int p=l1;
	while(in_order[p]!=root)p++;//p为中序遍历根节点位置
	int cnt=p-l1;//根节点左子树节点数量
	lch[root]=build(l1,p-1,l2,l2+cnt-1);
	rch[root]=build(p+1,r1,l2+cnt,r2-1);
	return root;//root存在,build成功
}
int best,best_sum;
void dfs(int u,int sum){
	sum+=u;
	if(!lch[u]&&!rch[u]){
		if(sum<best_sum||(sum==best_sum&&u<best)){//有多解的情况就判断最后根节点的权
			best=u;
			best_sum=sum;
		}
	}
	if(lch[u])dfs(lch[u],sum);
	if(rch[u])dfs(rch[u],sum);
}
int main(void){
	while(read_list(in_order)){
		read_list(post_order);
		build(0,n-1,0,n-1);
		best_num=1000000;
		dfs(post_order[n-1],0);
		cout<<best<<"\n";
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值