/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* rebuild(vector<int> &pre,int pre_l,int pre_r,vector<int> &vin,int vin_l,int vin_r){
        if(pre_l>pre_r)return NULL;//递归出口
        TreeNode* root = new TreeNode(pre[pre_l]);//建立头节点
        int vin_root=vin_l;
        while(vin[vin_root]!=pre[pre_l])vin_root++;//找到中序遍历中头节点下标
        root->left = rebuild(pre,pre_l+1,pre_l+vin_root-vin_l,vin,vin_l,vin_root-1);//建立左子树
        root->right = rebuild(pre,pre_l+vin_root-vin_l+1,pre_r,vin,vin_root+1,vin_r);//建立右子树
        return root;
    }
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
           return rebuild(pre,0,pre.size()-1,vin,0,vin.size()-1);//初始化函数
    }
};