DFS

## Code

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/
int maxPath(struct TreeNode *root,int *rec){
if(root==NULL)  return 0;
int left = maxPath(root->left,rec);
int right = maxPath(root->right,rec);
if(left<0)  left = 0;
if(right<0) right = 0;
*rec = *rec>left+right+root->val?*rec:left+right+root->val;
return (left>right?left:right)+root->val;
}
int maxPathSum(struct TreeNode* root) {
int max = 0x80000000;
maxPath(root,&max);
return max;
}

The elegance of algorithm must be tasted by racking your brain. : )

#### Binary Tree Maximum Path Sum -- LeetCode

2014-04-05 02:19:00

#### LeetCode（124） Binary Tree Maximum Path Sum

2015-12-27 16:20:25

#### LeetCode 124: Binary Tree Maximum Path Sum 树形动态规划

2015-09-02 15:52:14

#### LeetCode Binary Tree Maximum Path Sum 解题报告

2014-02-06 22:43:45

#### 【LeetCode】Binary Tree Maximum Path Sum 解题报告

2015-01-22 16:15:05

#### [leetcode-124]Binary Tree Maximum Path Sum(c++)

2015-08-12 15:49:49

#### Path Sum II -- LeetCode

2014-04-14 23:59:49

#### LeetCode 124:Binary Tree Maximum Path Sum

2015-12-30 22:36:02

#### LeetCode 124. Binary Tree Maximum Path Sum（二叉树最大路径和）

2016-05-27 00:27:32

#### [leetcode] 124. Binary Tree Maximum Path Sum

2015-07-03 13:58:25

## 不良信息举报

【leetcode】Binary Tree Maximum Path Sum