File tree 1 file changed +12
-13
lines changed
1 file changed +12
-13
lines changed Original file line number Diff line number Diff line change 7
7
* }
8
8
*/
9
9
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
-
23
10
/**
24
11
* @param {TreeNode } root Head of the binary tree.
25
12
* @return {max } Maximum path sum.
@@ -29,6 +16,18 @@ const maxValue = node => {
29
16
* Time O(k) - where k is height of tree.
30
17
*/
31
18
const 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
+
32
31
maxValue ( root ) ;
33
32
34
33
return max ;
You can’t perform that action at this time.
0 commit comments