LeetCode-Binary Tree Maximum Path Sum
2013-08-16 20:17
381 查看
/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function int maxSum = INT_MIN; maxSinglePathSum(root, maxSum); return maxSum; } int maxSinglePathSum(TreeNode *root, int &maxSum) { if (root == NULL) return 0; int leftMaxSum = max(0,maxSinglePathSum(root->left, maxSum)); int rightMaxSum = max(0,maxSinglePathSum(root->right, maxSum)); int curSum = root->val + leftMaxSum + rightMaxSum; maxSum = max(maxSum, curSum); return root->val + max(leftMaxSum, rightMaxSum); } };
相关文章推荐
- LeetCode刷题笔录Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- LeetCode_124 Binary Tree Maximum Path Sum
- leetcode_question_124 Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- leetcode: Binary Tree Maximum Path Sum
- LeetCode:Binary Tree Maximum Path Sum
- [LeetCode]Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum(LeetCode)
- LeetCode-- Binary Tree Maximum Path Sum
- Leetcode::Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [leetcode]Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [LeetCode]Binary Tree Maximum Path Sum, 解题报告
- LeetCode Binary Tree Maximum Path Sum
- leetcode — binary-tree-maximum-path-sum