先序遍历序列和中序遍历序列重建二叉树

//先序区间【preL,preR】,中序区间为【inL,inR】
 node *creat(int preL,int preR,int inL,int inR)
 {
 	if(preL>preR)
 	{
 		return NULL;
	 }
	node *root=new node;
	root->data=pre[preL];
	int k;
	for(int i=inL;i<=inR;i++)
	{
		if(pre[preL]==in[i])
		{
			k=i;
			break;
		}
	}
	int numleft=k-inL;     //根据数量去划分左右子树 
	root->lchild=creat(preL+1,preL+numleft,inL,k-1);
	root->rchild=creat(preL+numleft+1,preR,k+1,inR);
	return root;
 }
发布了13 篇原创文章 · 获赞 0 · 访问量 163

猜你喜欢

转载自blog.csdn.net/qq_43964401/article/details/103463611