Skip to content

Commit 06530e9

Browse files
Time: 53 ms (73.00%) | Memory: 48.3 MB (5.32%) - LeetSync
1 parent e1bfc8a commit 06530e9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
def nodesToSum(node):
9+
if not node:
10+
return 0
11+
node.sum = nodesToSum(node.left) + node.val + nodesToSum(node.right)
12+
return node.sum
13+
14+
def leftSplit(node):
15+
return (s - node.left.sum) * node.left.sum
16+
17+
def rightSplit(node):
18+
return (s - node.right.sum) * node.right.sum
19+
20+
def getMaxSplit(node):
21+
return max(max(leftSplit(node), getMaxSplit(node.left)) if node.left else 0,
22+
max(rightSplit(node), getMaxSplit(node.right)) if node.right else 0)
23+
24+
s = None
25+
26+
class Solution:
27+
def maxProduct(self, root: Optional[TreeNode]) -> int:
28+
global s
29+
30+
nodesToSum(root)
31+
s = root.sum
32+
return getMaxSplit(root) % (10**9+7)
33+

0 commit comments

Comments
 (0)