码迷,mamicode.com
首页 > 其他好文 > 详细

Construct Binary Tree from Inorder and Postorder Traversal || LeetCode

时间:2015-06-10 20:42:29      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

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

  

  

Construct Binary Tree from Inorder and Postorder Traversal || LeetCode

标签:

原文地址:http://www.cnblogs.com/ProtectedDream/p/4567025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!