/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) { struct TreeNode *root; int index=0; if(inorderSize<=0)return NULL; root=(struct TreeNode*)malloc(sizeof(struct TreeNode)); //root->left=root->right=NULL; root->val=postorder[postorderSize-1]; while(inorder[index]!=root->val)index++; root->left=buildTree(inorder,index,postorder,index); root->right=buildTree(inorder+index+1,inorderSize-index-1,postorder+index,postorderSize-index-1); return root; }
时间: 2024-12-20 16:01:31