由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 为啥有两个case不对??Binary Tree Maximum Path Sum
相关主题
请教大家一个问题:Maximum Height (Depth) of a Binary Tree Using PreOrder Traversalleetcode交了钱的能share一下题么?
[leetcode] Maximum Depth of Binary Treejava初学者问到leetcode sum root to leaf题
问一个题目判断 bst 疑问
check if a binary tree is a valid binary search tree问个google面试题
判断是不是binary search tree-leetcode问两道facebook面试题
help: leetcode "Recover Binary Search Tree" -- 附代码讨论一道LeetCode题:Binary Tree Maximum Path Sum
Find the node with given value in binary tree in in-orderBinary Tree Maximum Path Sum
Flatten Binary Tree to Linked List的recursive解法Leetcode bst max path-----is this solution correct?
相关话题的讨论汇总
话题: treenode话题: root话题: int
进入JobHunting版参与讨论
1 (共1页)
l********t
发帖数: 878
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< }
l*********8
发帖数: 4642
2
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

l********t
发帖数: 878
3
这个我留心了,不过好像不是啊。

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!

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

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

i**********e
发帖数: 1145
5
你的输入出了个小问题,应该是:
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

l********t
发帖数: 878
6
谢谢大牛火眼金睛!

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

l********t
发帖数: 878
7
另外谢谢 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;
}
};
1 (共1页)
进入JobHunting版参与讨论
相关主题
Leetcode bst max path-----is this solution correct?判断是不是binary search tree-leetcode
弱问:leetcode里Convert Sorted List to Binary Search Treehelp: leetcode "Recover Binary Search Tree" -- 附代码
flattern binary tree to linked list (leetcode)Find the node with given value in binary tree in in-order
leetcode Runtime error : Flatten Binary Tree to Linked ListFlatten Binary Tree to Linked List的recursive解法
请教大家一个问题:Maximum Height (Depth) of a Binary Tree Using PreOrder Traversalleetcode交了钱的能share一下题么?
[leetcode] Maximum Depth of Binary Treejava初学者问到leetcode sum root to leaf题
问一个题目判断 bst 疑问
check if a binary tree is a valid binary search tree问个google面试题
相关话题的讨论汇总
话题: treenode话题: root话题: int