题解 | #子数组的最大累加和问题#

重建二叉树

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>

}

};

全部评论

相关推荐

Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务