问题描述:
根据一棵树的后序遍历和中序遍历构造一颗二叉树。
问题分析:
采用传统的递归方式求解:
class Solution{
public:
unordered_map<int, int> in_pos; //构建构建构建hashmap
vector<int> post;
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
int in_len = inorder.size();
for(int i= 0; i<in_len;i++)
{
in_pos[inorder[i]] =i;
}
post = postorder;
TreeNode* root = buildTree(0,in_len-1,0,post.size()-1);
return root;
}
TreeNode* buildTree(int in_left, int in_right, int post_left,int post_right)
{
if(in_left>in_right || post_left>post_right) return NULL;
int root_val = post[post_right];
int root_pos = in_pos[root_val];
TreeNode* node = new TreeNode(root_val);
node->left = buildTree(in_left,root_pos-1,post_left,post_left+root_pos-in_left-1); //左子树上的边界确定
node->right = buildTree(root_pos+1,in_right,post_left+root_pos-in_left,post_right-1); //确定右子树的两边界(对于inorder 和postorder遍历来说)
return node;
}
};