Skip to content

Commit e73ad22

Browse files
authored
Create maximum-product-of-splitted-binary-tree.py
1 parent 3dc45a9 commit e73ad22

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: Python/maximum-product-of-splitted-binary-tree.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(n)
2+
# Space: O(h)
3+
4+
# Definition for a binary tree node.
5+
class TreeNode(object):
6+
def __init__(self, x):
7+
self.val = x
8+
self.left = None
9+
self.right = None
10+
11+
12+
class Solution(object):
13+
def maxProduct(self, root):
14+
"""
15+
:type root: TreeNode
16+
:rtype: int
17+
"""
18+
MOD = 10**9 + 7
19+
def dfs(root, total, result):
20+
if not root:
21+
return 0
22+
subtotal = dfs(root.left, total, result)+dfs(root.right, total, result)+root.val
23+
result[0] = max(result[0], subtotal*(total-subtotal) )
24+
return subtotal
25+
26+
result = [0]
27+
dfs(root, dfs(root, 0, result), result)
28+
return result[0] % MOD

0 commit comments

Comments
 (0)