/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* buidl_tree(int* inorder,int head1,int n1,int* postorder,int head2,int n2){
if(n1<=0||n2<=0) return NULL;
struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val=postorder[head2+n2-1];
int i=head1;
for(;i<head1+n1;i++){
if(inorder[i]==root->val) break;
}
root->left=buidl_tree(inorder,head1,i-head1,postorder,head2,i-head1);
root->right=buidl_tree(inorder,i+1,n1-i+head1-1,postorder,head2+i-head1,n2-i+head1-1);
return root;
}
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
return buidl_tree(inorder,0,inorderSize,postorder,0,postorderSize);
}
结果:
标签:遍历,TreeNode,struct,head1,int,inorder,二叉树,106,postorder From: https://www.cnblogs.com/llllmz/p/18056298