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

  

  

posted on 2015-06-10 18:35  coderlt  阅读(104)  评论(0编辑  收藏  举报

导航