struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){ if(preorderSize==0 || inorderSize==0) return NULL; struct TreeNode** stArr=(struct TreeNode**)calloc(preorderSize,sizeof(struct TreeNode*)); int top=-1, pPst=0, iPst=0, flag=iPst; struct TreeNode* root=(struct TreeNode*)calloc(1,sizeof(struct TreeNode)); struct TreeNode* temp=root; root->val=preorder[pPst++]; stArr[++top]=root; while(pPst<preorderSize && iPst<inorderSize){ while(top>=0 && stArr[top]->val == inorder[iPst]){ temp=stArr[top--]; iPst++; } struct TreeNode* node=(struct TreeNode*)calloc(1,sizeof(struct TreeNode)); node->val=preorder[pPst++]; stArr[++top]=node; if(flag != iPst) temp->right=node; else temp->left=node; temp=node; flag=iPst; } return root; }