Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1b03ce

Browse files
committed
Add solution 124.
1 parent 9ee0278 commit f1b03ce

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* public $val = null;
5+
* public $left = null;
6+
* public $right = null;
7+
* function __construct($value) { $this->val = $value; }
8+
* }
9+
*/
10+
class Solution {
11+
12+
var $maxSum = -2147483647;
13+
/**
14+
* @param TreeNode $root
15+
* @return Integer
16+
*/
17+
function maxPathSum($root) {
18+
self::helper($root);
19+
return $this->maxSum;
20+
}
21+
22+
function helper($root) {
23+
if($root == null) return 0;
24+
$left = self::helper($root->left);
25+
$right = self::helper($root->right);
26+
$rootLeft = $left + $root->val;
27+
$rootRight = $right + $root->val;
28+
$ret = max(max($rootLeft, $rootRight), $root->val);
29+
$compSum = $root->val;
30+
if($left > 0) $compSum += $left;
31+
if($right > 0) $compSum += $right;
32+
$this->maxSum = max($this->maxSum, $compSum);
33+
return $ret;
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /