关于二叉树建树
  • 板块灌水区
  • 楼主zymooll
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/4/2 20:49
  • 上次更新2023/11/5 01:09:06
查看原帖
关于二叉树建树
289296
zymooll楼主2021/4/2 20:49

虽然按着书上建树做完了P4715,但是还是没有看懂建树原理,望神犇解答 code:

#include<bits/stdc++.h>
using namespace std;
int n,ls,js;
typedef struct node;
typedef node* tree;
struct data1{
	int gj,sl;
};
struct node{
	data1 data;
	tree child[2];
};
inline data1 cmp(data1 a1,data1 a2){
	if(a1.sl>a2.sl){
		return a1;
	}
	else{
		return a2;
	}
}
inline int cmp2(data1 a1,data1 a2){
	if(a1.sl>a2.sl){
		return a2.gj;
	}
	else{
		return a1.gj;
	}
}
void csh(tree &t,int sd){
	if(sd!=n){
		t=new node;
		data1 lls={-1,-1};
		t->data=lls;
		csh(t->child[0],sd+1);
		csh(t->child[1],sd+1);
	}
	else{
		t=new node;
		cin>>ls;
		data1 lls={++js,ls};
		t->data=lls;
	}
	return;
}
void cal(tree &t,int sd){
	if(sd!=0){
		if(sd!=n-1){
			cal(t->child[0],sd+1);
			cal(t->child[1],sd+1);
		}
		data1 lls=cmp(t->child[0]->data,t->child[1]->data);
		t->data=lls;
	}
	else{
		cal(t->child[0],sd+1);
		cal(t->child[1],sd+1);
	}
	return;
}
int main(){
	tree t;
	cin>>n;
	csh(t,0);
	if(n!=1)cal(t,0);
	cout<<cmp2(t->child[0]->data,t->child[1]->data);
	return 0;
}

thx

2021/4/2 20:49
加载中...