中级农民
- 积分
- 202
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-11-22
- 最后登录
- 1970-1-1
|
宝宝不哭,我给你写了一个返回路径的
int helper(TreeNode* root, int& max_path, vector<int>& max_path_vec, vector<int>& path){
if(!root) return 0;
vector<int> left_path, right_path;
int left = max(helper(root->left, max_path, max_path_vec, left_path), 0);
int right = max(helper(root->right, max_path, max_path_vec, right_path), 0);
if(max_path < root->val + left + right){
max_path = root->val + left + right;
max_path_vec.clear();
max_path_vec.insert(max_path_vec.end(), left_path.rbegin(), left_path.rend());
max_path_vec.push_back(root->val);
max_path_vec.insert(max_path_vec.end(), right_path.begin(), right_path.end());
}
path.clear();
path.push_back(root->val);
if(left > right){
path.insert(path.end(), left_path.begin(), left_path.end());
}
else path.insert(path.end(), right_path.begin(), right_path.end());
return root->val + max(left, right);
}
|
|