Construct Binary Tree from Inorder and Postorder Traversal || LeetCode

/**
* 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;
}

  

  

上一篇:java并发之hashmap


下一篇:Android LayoutParams