重建二叉树_牛客题霸_牛客网
TreeNode* reConstructBinaryTree(vector<int>& preOrder, vector<int>& vinOrder) {// write code hereint n = preOrder.size();int m = vinOrder.size();if(n == 0 || m == 0) return NULL;TreeNode* root = new TreeNode(preOrder[0]);for(int i=0; i<vinOrder.size(); i++){if(preOrder[0] == vinOrder[i]){vector<int>leftpre(preOrder.begin()+1, preOrder.begin()+i+1);vector<int>leftvin(vinOrder.begin(), vinOrder.begin() + i);root->left = reConstructBinaryTree(leftpre, leftvin);vector<int>rightpre(preOrder.begin()+i+1, preOrder.end());vector<int>rightvin(vinOrder.begin() +i+1, vinOrder.end());root->right = reConstructBinaryTree(rightpre, rightvin);break;}}return root;}