File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {TreeNode } root
10
+ * @return {number }
11
+ */
12
+ var maxProduct = function ( root ) {
13
+ let a = 0 ;
14
+ let b = 0 ;
15
+
16
+ let sum = 0 ;
17
+ function dfs ( node ) {
18
+ if ( ! node ) {
19
+ return 0 ;
20
+ }
21
+ sum += node . val ;
22
+ node . lSum = dfs ( node . left ) ;
23
+ node . rSum = dfs ( node . right ) ;
24
+ return node . val + node . lSum + node . rSum ;
25
+ }
26
+ dfs ( root ) ;
27
+
28
+ let result = 0 ;
29
+ function cal ( node ) {
30
+ result = Math . max ( result , node . lSum * ( sum - node . lSum ) , ( sum - node . rSum ) * node . rSum ) ;
31
+ if ( node . left ) {
32
+ cal ( node . left ) ;
33
+ }
34
+ if ( node . right ) {
35
+ cal ( node . right ) ;
36
+ }
37
+ }
38
+ cal ( root ) ;
39
+
40
+ return result % 1000000007 ;
41
+ } ;
You can’t perform that action at this time.
0 commit comments