题解 | #子数组的最大累加和问题#
重建二叉树
http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
if(pre.empty() || vin.empty()) return nullptr;
int root_val = pre[0];
int vin_index;
for(vin_index = 0; vin_index<vin.size(); vin_index++){
if(vin[vin_index] == root_val) break;
}
TreeNode* root = new TreeNode(vin[vin_index]);
vector<int> left_vin(vin.begin(), vin.begin()+vin_index);//切割中序
vector<int> right_vin(vin.begin()+vin_index+1, vin.end());
vector<int> left_pre(pre.begin()+1, pre.begin()+1+vin_index);//切割后序
vector<int> right_pre(pre.begin()+1+vin_index, pre.end());
root->left = reConstructBinaryTree(left_pre, left_vin);
root->right = reConstructBinaryTree(right_pre, right_vin);
return root;</int></int></int></int></int></int>
}
};