Create 由先序中序建立二叉树.cpp

This commit is contained in:
hao14293
2019-07-25 16:02:09 +08:00
committed by GitHub
parent b0917712fc
commit fe9efd6ea2

View File

@@ -0,0 +1,18 @@
BiTree PreInCreat(ElemType A[], ElemType B[], int l1, int h1, int l2, int h2){
//初始调用 l1 = l2 = 1, h1 = h2 = n;
root = (BiTNode*)malloc(sizeof(BiTNode));
root->data = A[l1];
for(int i = l2; B[i] != root->data; i++);
llen = i - l2;
rlen = h2 - i;
if(llen)
root->lchild = PreInCreat(A, B, l1 + 1, l1 + llen, l2, l2 + llen - 1);
else
root->lchild = NULL;
if(rlen)
root->rchild = PreInCreat(A, B, h1 - rlen + 1, h1, h2 - rlen + 1, h2);
else
root->rchild = NULL;
return root;
}