File tree Expand file tree Collapse file tree 1 file changed +12
-13
lines changed
Expand file tree Collapse file tree 1 file changed +12
-13
lines changed Original file line number Diff line number Diff line change 77 * }
88 */
99
10- let max = Number . MIN_SAFE_INTEGER ;
11-
12- const maxValue = node => {
13- if ( node === null ) return 0 ;
14-
15- let leftValue = Math . max ( maxValue ( node . left ) , 0 ) ;
16- let rightValue = Math . max ( maxValue ( node . right ) , 0 ) ;
17-
18- max = Math . max ( max , node . val + leftValue + rightValue ) ;
19-
20- return node . val + Math . max ( leftValue , rightValue ) ;
21- } ;
22-
2310/**
2411 * @param {TreeNode } root Head of the binary tree.
2512 * @return {max } Maximum path sum.
@@ -29,6 +16,18 @@ const maxValue = node => {
2916 * Time O(k) - where k is height of tree.
3017 */
3118const maxPathSum = root => {
19+ let max = Number . MIN_SAFE_INTEGER ;
20+ const maxValue = node => {
21+ if ( node === null ) return 0 ;
22+
23+ let leftValue = Math . max ( maxValue ( node . left ) , 0 ) ;
24+ let rightValue = Math . max ( maxValue ( node . right ) , 0 ) ;
25+
26+ max = Math . max ( max , node . val + leftValue + rightValue ) ;
27+
28+ return node . val + Math . max ( leftValue , rightValue ) ;
29+ } ;
30+
3231 maxValue ( root ) ;
3332
3433 return max ;
You can’t perform that action at this time.
0 commit comments