geeksforgeeks Find the maximum path sum between two leaves of a binary tree
2014-07-28 07:36
603 查看
Geeksforgeeks上面的一道题: http://www.geeksforgeeks.org/find-maximum-path-sum-two-leaves-binary-tree/
Geeksforgeeks题目描述和解答已经非常清楚,只是那里的实现有bug,见后面的评论。在所有节点val为负或者只有一条边其 val为负的情况下,原实现都返回错误结果。原因是为空的时候返回0这个简单设想覆盖了正确答案。尽管很多人指出了这个问题,但截止目前(2014/7/27),geeksforgeeks还没有更正。
Geeksforgeeks题目描述和解答已经非常清楚,只是那里的实现有bug,见后面的评论。在所有节点val为负或者只有一条边其 val为负的情况下,原实现都返回错误结果。原因是为空的时候返回0这个简单设想覆盖了正确答案。尽管很多人指出了这个问题,但截止目前(2014/7/27),geeksforgeeks还没有更正。
#include <iostream> using namespace std; // #define INT_MIN (-2147483647 - 1) struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode (int val) { this->val = val; this->left = NULL; this->right = NULL; } }; class Solution { public: int maxPathSum(TreeNode *root) { int maxsum = INT_MIN; maxPathSum(root, maxsum); return maxsum; } int maxPathSum(TreeNode *root, int &maxsum) { if (root->left == NULL && root->right == NULL) { maxsum = max(maxsum, root->val); return root->val; } else if (root->left == NULL) { int r = maxPathSum(root->right, maxsum); maxsum = max(maxsum, r); return root->val + r; } else if (root->right == NULL) { int l = maxPathSum(root->left, maxsum); maxsum = max(maxsum, l); return l + root->val; } else { int l = maxPathSum(root->left, maxsum); int r = maxPathSum(root->right, maxsum); maxsum = max(maxsum, l + root->val + r); return max(l, r) + root->val; } } void printTree(TreeNode *root) { if (root) { cout<<root->val<<endl; printTree(root->left); printTree(root->right); } } }; int main() { // TreeNode *root = new TreeNode(-15); // root->left = new TreeNode(5); // root->right = new TreeNode(6); // root->left->left = new TreeNode(-8); // root->left->right = new TreeNode(1); // root->left->left->left = new TreeNode(2); // root->left->left->right = new TreeNode(6); // root->right->left = new TreeNode(3); // root->right->right = new TreeNode(9); // root->right->right->right = new TreeNode(0); // root->right->right->right->left = new TreeNode(4); // root->right->right->right->right = new TreeNode(-1); // root->right->right->right->right->left = new TreeNode(10); // TreeNode *root = new TreeNode(-3); // printTree(root); TreeNode *root = new TreeNode(-3); root->right = new TreeNode(8); root->left = new TreeNode(4); Solution s; cout<<s.maxPathSum(root)<<endl; return 0; }
相关文章推荐
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
- Find the nearest common ancestor of any two nodes in a binary search tree
- Java for LeetCode 124 Binary Tree Maximum Path Sum
- [124]. Binary Tree Maximum Path Sum, [543]. Diameter of Binary Tree
- [geeks]Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST
- Find the common ancestor of two nodes in a binary tree
- Find distance between two given keys of a Binary Tree
- leetcode4: Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- leetcode - Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- Problem 10 of Find the sum of all the primes below two million.
- 【leetcode】Binary Tree Maximum Path Sum
- LeetCode——Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum Solution
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum[leet code test cases passed]
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- LeetCode: Binary Tree Maximum Path Sum