avatar
l*t
1
Judge small 全对
Judge large, 一共92个test case,有90个是对滴,
其中一个错误的是这个test case
{9,6,-3,#,#,-6,2,#,#,2,#,-6,-6,-6}
还有一个太长,不写了。
我自己编译运行结果是16,也是正确答案。上OJ非说我的结果是15...
leetcode,或者哪位大牛给看看?谢谢啊!
/**
* Definition for binary tree */
#include
#include
#include
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int maxValue;
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int singleDown;
maxValue = -9999;
return maxPathOverload(root, singleDown);
}
int maxPathOverload(TreeNode* root, int& singleLine){
if (!root){
singleLine = 0;
return 0;
}
int leftSingleLine = 0;
int rightSingleLine = 0;
maxPathOverload(root->left, leftSingleLine);
maxPathOverload(root->right, rightSingleLine);
singleLine = root->val + max(leftSingleLine , rightSingleLine);
vector subMaxPath;
subMaxPath.push_back(root->val);
subMaxPath.push_back(root->val + leftSingleLine);
subMaxPath.push_back(root->val + rightSingleLine);
subMaxPath.push_back(root->val + leftSingleLine + rightSingleLine);
maxValue = max(maxValue, *max_element(subMaxPath.begin(), subMaxPath
.end()));
//compare the path values, which you HAVE TO include the current
node
//This eleminate the zero's for null leaf's

return maxValue;
}
};
//{9,6,-3,#,#,-6,2,#,#,2,#,-6,-6,-6}
int main(){
TreeNode n1(9);
TreeNode n1L(6);
TreeNode n1R(-3);
TreeNode n2L(-6);
TreeNode n2R(2);
TreeNode n3L(2);
TreeNode n4L(-6);
TreeNode n4R(-6);
TreeNode n5L(-6);
n1.left = &n1L;
n1.right = &n1R;
n1R.left = &n2L;
n1R.right = &n2R;
n2R.left = &n3L;
n3L.left = &n4L;
n4L.right = &n4R;
n4R.left = &n5L;
Solution s;
int result = 0;
result = s.maxPathSum(&n1);
cout<}
avatar
S*M
2
刚刚看到的Susan Sontag的话,很猛
怪不得AL喜欢她
There is something predatory in the act of taking a picture. To photograph
people is to violate them, by seeing them as they never see themselves" (
1977,p14)
in the book, On Photography
avatar
r*e
3
avatar
l*8
4
This is the reason?
IMPORTANT:
The Solution object is instantiated only once and is reused for each
test case input. When declaring a class member variable, be extra cautious
and remember to reset the variable!

【在 l********t 的大作中提到】
: Judge small 全对
: Judge large, 一共92个test case,有90个是对滴,
: 其中一个错误的是这个test case
: {9,6,-3,#,#,-6,2,#,#,2,#,-6,-6,-6}
: 还有一个太长,不写了。
: 我自己编译运行结果是16,也是正确答案。上OJ非说我的结果是15...
: leetcode,或者哪位大牛给看看?谢谢啊!
: /**
: * Definition for binary tree */
: #include

avatar
c*s
5
大学时候写了篇essay,讨论on photography。 教授看完了说了他没看懂我想说什么。
我其实是想说我没看懂susan sontag想说什么
avatar
l*t
6
这个我留心了,不过好像不是啊。

each

【在 l*********8 的大作中提到】
: This is the reason?
: IMPORTANT:
: The Solution object is instantiated only once and is reused for each
: test case input. When declaring a class member variable, be extra cautious
: and remember to reset the variable!

avatar
T*t
7
贴出来大家看看呗

【在 c******s 的大作中提到】
: 大学时候写了篇essay,讨论on photography。 教授看完了说了他没看懂我想说什么。
: 我其实是想说我没看懂susan sontag想说什么

avatar
l*8
8
好像跟我以前发现的bug是一样的。
singleLine = root->val + max(leftSingleLine , rightSingleLine);
改成
singleLine = max(0, root->val + max(leftSingleLine , rightSingleLine));

【在 l********t 的大作中提到】
: 这个我留心了,不过好像不是啊。
:
: each

avatar
S*M
9
赞文艺青年

【在 c******s 的大作中提到】
: 大学时候写了篇essay,讨论on photography。 教授看完了说了他没看懂我想说什么。
: 我其实是想说我没看懂susan sontag想说什么

avatar
i*e
10
你的输入出了个小问题,应该是:
n3L.right = &n4R;
n4L.left = &n5L;
这样运行你的程序就会 output 15。

【在 l********t 的大作中提到】
: Judge small 全对
: Judge large, 一共92个test case,有90个是对滴,
: 其中一个错误的是这个test case
: {9,6,-3,#,#,-6,2,#,#,2,#,-6,-6,-6}
: 还有一个太长,不写了。
: 我自己编译运行结果是16,也是正确答案。上OJ非说我的结果是15...
: leetcode,或者哪位大牛给看看?谢谢啊!
: /**
: * Definition for binary tree */
: #include

avatar
l*t
11
谢谢大牛火眼金睛!

【在 i**********e 的大作中提到】
: 你的输入出了个小问题,应该是:
: n3L.right = &n4R;
: n4L.left = &n5L;
: 这样运行你的程序就会 output 15。

avatar
l*t
12
另外谢谢 longway2008,确实是算单线最长的时候没考虑周到。贴个改过后work的
class Solution {
public:
int maxValue;
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int singleDown;
maxValue = -9999;
return maxPathOverload(root, singleDown);
}
int maxPathOverload(TreeNode* root, int& singleLine){
if (!root){
singleLine = 0;
return 0;
}
int leftSingleLine = 0;
int rightSingleLine = 0;
maxPathOverload(root->left, leftSingleLine);
maxPathOverload(root->right, rightSingleLine);
vector subMaxPath;
subMaxPath.push_back(root->val);
subMaxPath.push_back(root->val + leftSingleLine);
subMaxPath.push_back(root->val + rightSingleLine);
singleLine = *max_element(subMaxPath.begin(), subMaxPath.end());
subMaxPath.push_back(root->val + leftSingleLine + rightSingleLine);
maxValue = max(maxValue, *max_element(subMaxPath.begin(), subMaxPath
.end()));
//compare the path values, which you HAVE TO include the current
node
//This eleminate the zero's for null leaf's

return maxValue;
}
};
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。