@@ -12,58 +12,60 @@ class TreeNode {
12
12
* @return {number }
13
13
*/
14
14
function maxPathSum ( root ) {
15
- let queue = [ root ] , max = root . val ;
15
+ let max = root . val ;
16
16
17
17
const exploreToFindMax = ( node ) => {
18
- if ( node === null ) return ;
18
+ if ( node === null ) return 0 ;
19
19
console . log ( `-> v:${ node . val } ` )
20
20
21
- // let l = node.left, r = node.right;
22
- // if (l !== null) {}
23
- const defaultForNull = - Infinity ;
21
+ let exploreLeftVal = exploreToFindMax ( node . left ) ,
22
+ exploreRightVal = exploreToFindMax ( node . right ) ;
23
+ console . log ( `exploreLeftVal: ${ exploreLeftVal } , exploreRightVal: ${ exploreRightVal } ` ) ;
24
24
25
- let treeLeftVal = 0 , exploreLeftVal = defaultForNull ;
26
- if ( node . left !== null ) {
27
- treeLeftVal = node . left . val ;
28
- // console.log("exploreToFindMax -> l")
29
- exploreLeftVal = node . val + exploreToFindMax ( node . left ) ;
30
- }
31
- // console.log("treeLeftVal")
32
- // console.log(treeLeftVal)
33
- // console.log("exploreLeftVal")
34
- // console.log(exploreLeftVal)
25
+ // 0 represents not picking any of them
26
+ // let treeMax = node.val + treeLeftVal + treeRightVal;
27
+ // let treeMax = node.val + (node.left?.val ?? 0) + (node.right?.val ?? 0);
28
+ let treeMax = node . val + exploreLeftVal + exploreRightVal ;
35
29
36
- let treeRightVal = 0 , exploreRightVal = defaultForNull ;
37
- if ( node . right !== null ) {
38
- treeRightVal = node . right . val ;
39
- console . log ( "exploreToFindMax -> r" )
40
- exploreRightVal = node . val + exploreToFindMax ( node . right ) ;
41
- console . log ( "exploreRightVal" )
42
- console . log ( exploreRightVal )
43
- }
44
-
45
- let treeMax = node . val + treeLeftVal + treeRightVal ; // 0 represents not picking any of them
46
-
47
- max = Math . max ( max , treeMax , exploreLeftVal , exploreRightVal ) ;
48
- console . log ( "node.val" )
49
- console . log ( node . val )
30
+ // max = Math.max(max, treeMax, node.val + Math.max(exploreLeftVal, exploreRightVal));
31
+ max = Math . max ( max , treeMax ) ;
50
32
console . log ( "node.val + Math.max(exploreLeftVal, exploreRightVal, 0)" )
51
- console . log ( node . val + Math . max ( exploreLeftVal , exploreRightVal , 0 ) )
52
- return node . val + Math . max ( exploreLeftVal , exploreRightVal , 0 ) ;
33
+ console . log ( node . val + Math . max ( exploreLeftVal , exploreRightVal ) )
34
+
35
+ return node . val + Math . max ( exploreLeftVal , exploreRightVal ) ;
36
+ return Math . max ( node . val + Math . max ( exploreLeftVal , exploreRightVal ) , 0 ) ;
53
37
} ;
54
38
55
- while ( queue . length > 0 ) {
56
- let curr = queue . shift ( ) , left = curr . left , right = curr . right ;
57
- if ( left !== null ) queue . push ( left ) ;
58
- if ( right !== null ) queue . push ( right ) ;
59
- exploreToFindMax ( curr ) ;
60
- }
39
+ exploreToFindMax ( root ) ;
61
40
62
41
console . log ( "max" )
63
42
console . log ( max )
64
43
return max ;
65
44
}
66
45
46
+ // [-1,-2,10,-6,null,-3,-6]
47
+ // maxPathSum(
48
+ // new TreeNode(-1,
49
+ // new TreeNode(-2,
50
+ // new TreeNode(-6)
51
+ // ),
52
+ // new TreeNode(10,
53
+ // new TreeNode(-3),
54
+ // new TreeNode(-6)
55
+ // )
56
+ // )
57
+ // )
58
+
59
+ // maxPathSum(
60
+ // new TreeNode(-10,
61
+ // new TreeNode(9),
62
+ // new TreeNode(20,
63
+ // new TreeNode(15),
64
+ // new TreeNode(7)
65
+ // )
66
+ // )
67
+ // )
68
+
67
69
maxPathSum (
68
70
new TreeNode ( 1 ,
69
71
new TreeNode ( - 2 ) ,
0 commit comments