# Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.

Construct Binary Tree from Preorder and Inorder Traversal思路基本一致，主要是根据在中序遍历中找到当前的值，然后通过对应关系递归。

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return creatTree(0, inorder.size() - 1, 0, postorder.size() - 1, inorder, postorder);
}
TreeNode*creatTree(int in_begin, int in_end, int post_begin,int post_end, vector<int> &inorder, vector<int> &postorder){
if(post_begin > post_end)    return NULL;
TreeNode* root = new TreeNode(postorder[post_end]);
int index = 0;
for(int i = in_end; i >= 0; --i){
if(inorder[i] == root -> val){
index = i;
break;
}
}
root -> right = creatTree(index + 1, in_end, post_begin + index - in_begin, post_end - 1, inorder, postorder);
root -> left = creatTree(in_begin, index - 1, post_begin, post_begin + index - in_begin - 1, inorder, postorder);
return root;
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：Construct Binary Tree from Inorder and Postorder Traversal 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)