Leetcode::Binary Tree Maximum Path Sum
2013-06-28 20:45
369 查看
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #define neg -10000 struct Node { int val1; int val2; Node():val1(neg),val2(neg){}; }; class Solution { public: Node maxPath(TreeNode *root ) { Node result; if(root->left == NULL && root->right == NULL) { result.val1=result.val2=root->val; return result; } Node LN; Node RN; if( root->left != NULL) { LN=maxPath(root->left); } if( root->right != NULL ) { RN=maxPath(root->right); } int tmp=max(LN.val1,RN.val1); if( (root->val >0 || root->val < 0) && tmp < 0) { result.val1=root->val; } else { result.val1=root->val + tmp; } result.val2=max(max(max(LN.val2,RN.val2),LN.val1+RN.val1+root->val),result.val1); return result; } int maxPathSum(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function Node result=maxPath(root); return result.val2; } };
相关文章推荐
- LeetCode124 Binary Tree Maximum Path Sum
- leetcode 日经贴,Cpp code -Binary Tree Maximum Path Sum
- Leetcode 124 Binary Tree Maximum Path Sum
- [leetcode刷题系列]Binary Tree Maximum Path Sum
- [leetcode-14]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 – Refresh – Binary Tree Maximum Path Sum
- LeetCode "Binary Tree Maximum Path Sum"
- LeetCode Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum----leetcode
- leetcode || 124、Binary Tree Maximum Path Sum
- Leetcode 124 Binary Tree Maximum Path Sum 二叉树最大路径和
- LeetCode: Binary Tree Maximum Path Sum 解题报告