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

重建二叉树

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>

}

};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-15 17:17
点赞 评论 收藏
分享
06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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