1212
1313public class Solution {
1414
15- public class MyInt {
15+ public class MyInt {
1616
17- private int value ;
17+ private int value ;
1818
19- public int getValue () {
20- return value ;
21- }
22-
23- public void setValue (int value ) {
24- this .value = value ;
25- }
19+ public int getValue () {
20+ return value ;
21+ }
2622
27- public MyInt (int value ) {
28- this .value = value ;
29- }
23+ public void setValue (int value ) {
24+ this .value = value ;
25+ }
3026
27+ public MyInt (int value ) {
28+ this .value = value ;
3129 }
30+
31+ }
3232
33- public int maxPathSum (TreeNode root ) {
34- MyInt maxSum = new MyInt (Integer .MIN_VALUE );
35- findMaxSum (root , maxSum );
36- return maxSum .getValue ();
37- }
33+ public int maxPathSum (TreeNode root ) {
34+ MyInt maxSum = new MyInt (Integer .MIN_VALUE );
35+ findMaxSum (root , maxSum );
36+ return maxSum .getValue ();
37+ }
3838
39- public int findMaxSum (TreeNode root , MyInt maxSum ) {
39+ public int findMaxSum (TreeNode root , MyInt maxSum ) {
4040 if (root == null )
41- return 0 ;
42- int cpath = 0 ;
43- int left = findMaxSum (root .left , maxSum );
44- int right = findMaxSum (root .right , maxSum );
45- cpath = Math .max (root .val , Math .max (root .val + left , root .val + right ));
46- maxSum .setValue (Math .max (maxSum .getValue (), Math .max (root .val + left + right , cpath )));
47- return cpath ;
48- }
41+ return 0 ;
42+ int cpath = 0 ;
43+ int left = findMaxSum (root .left , maxSum );
44+ int right = findMaxSum (root .right , maxSum );
45+ cpath = Math .max (root .val , Math .max (root .val + left , root .val + right ));
46+ maxSum .setValue (Math .max (maxSum .getValue (), Math .max (root .val + left + right , cpath )));
47+ return cpath ;
48+ }
4949}
0 commit comments