Skip to content

Commit 277cbec

Browse files
committed
feat: add question 1339
1 parent bf38260 commit 277cbec

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
};

0 commit comments

Comments
 (0)