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

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

猜你喜欢

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