由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Leetcode bst max path-----is this solution correct?
相关主题
讨论一道LeetCode题:Binary Tree Maximum Path Sumleetcode Runtime error : Flatten Binary Tree to Linked List
[leetcode] Maximum Depth of Binary Treeyahoo onsite
help: leetcode "Recover Binary Search Tree" -- 附代码问一个leetcode上面binary tree的题目
Find the node with given value in binary tree in in-order[leetcode] Minimum Depth of Binary Tree 我的这个答案说wrong answer,但是我在本地跑就是对的.
Binary Tree Maximum Path Sum这个题做的对吗?
Lowest common ancestor of two nodes of Binary Treeleetcode交了钱的能share一下题么?
弱问:leetcode里Convert Sorted List to Binary Search Tree求教Leetcode题目:Lowest Common Ancestor
check if a binary tree is a valid binary search treeFind a sub tree with min weight怎么做
相关话题的讨论汇总
话题: maxsofar话题: int话题: treenode话题: maxpath话题: node
进入JobHunting版参与讨论
1 (共1页)
T******7
发帖数: 1419
1
//==========================================================================
==
// Binary Tree Maximum Path Sum
// Given a binary tree, find the maximum path sum.
//
// The path may start and end at any node in the tree.
//
// For example:
// Given the below binary tree,
//
// " 1 "
// " / \ "
// " 2 3 "
//
// Return 6.
//
//==========================================================================
==
#include
using namespace std;
/**
* Definition for binary tree
*/
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
int maxPathSum(TreeNode *root)
{
if (root == NULL) return 0;
int maxSoFar = root->val;
maxPathSumHelper(root, maxSoFar);
return maxSoFar;
}
int maxPathSumHelper(TreeNode *node, int &maxSoFar)
{
if (node == NULL) return 0;
int leftMax = maxPathSumHelper(node->left, maxSoFar);
int rightMax = maxPathSumHelper(node->right, maxSoFar);
int maxPath = node->val;
if (leftMax > 0) maxPath += leftMax;
if (rightMax > 0) maxPath += rightMax;
if (maxPath > maxSoFar) maxSoFar = maxPath;
int res = node->val;
return max(res, res+max(leftMax, rightMax));
}
};
int main()
{
return 0;
}
Y**3
发帖数: 21
2
我的代码和你的几乎一样
p*g
发帖数: 141
3
为什么不自己试试能不能过leetcode 的OJ
我当时调试了一阵, 最终过了
如果你fail了几个case,那很可能你的算法有地方有问题

==

【在 T******7 的大作中提到】
: //==========================================================================
: ==
: // Binary Tree Maximum Path Sum
: // Given a binary tree, find the maximum path sum.
: //
: // The path may start and end at any node in the tree.
: //
: // For example:
: // Given the below binary tree,
: //

n******e
发帖数: 957
4
兄弟你这不叫bst啊。。。
P******r
发帖数: 842
5
我的logic也是几乎一样的。
1 (共1页)
进入JobHunting版参与讨论
相关主题
Find a sub tree with min weight怎么做Binary Tree Maximum Path Sum
判断 bst 疑问Lowest common ancestor of two nodes of Binary Tree
问一个题目弱问:leetcode里Convert Sorted List to Binary Search Tree
弱问怎么判断两个binary tree相同?check if a binary tree is a valid binary search tree
讨论一道LeetCode题:Binary Tree Maximum Path Sumleetcode Runtime error : Flatten Binary Tree to Linked List
[leetcode] Maximum Depth of Binary Treeyahoo onsite
help: leetcode "Recover Binary Search Tree" -- 附代码问一个leetcode上面binary tree的题目
Find the node with given value in binary tree in in-order[leetcode] Minimum Depth of Binary Tree 我的这个答案说wrong answer,但是我在本地跑就是对的.
相关话题的讨论汇总
话题: maxsofar话题: int话题: treenode话题: maxpath话题: node