-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathmax-path-sum-binary-trees.py
85 lines (67 loc) · 2.69 KB
/
max-path-sum-binary-trees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# MAX PATH SUM IN BINARY TREES
# O(N) time and space
def maxPathSum(tree):
# Write your code here.
root = calculateMaxPathSum(tree)
return root.maxPathSum
def calculateMaxPathSum(node):
if not node:
return TreeInfo()
leftSubtree = calculateMaxPathSum(node.left)
rightSubtree = calculateMaxPathSum(node.right)
maxLeftPathSum = max(leftSubtree.maxLeftSum + node.value, leftSubtree.maxRightSum + node.value)
maxRightPathSum = max(rightSubtree.maxLeftSum + node.value, rightSubtree.maxRightSum + node.value)
currentMaxPathSum = maxLeftPathSum + maxRightPathSum - node.value
maxPathSumFromSubtrees = max(leftSubtree.maxPathSum, rightSubtree.maxPathSum)
maxPathSumFromCurrentNode = max(currentMaxPathSum, maxPathSumFromSubtrees, maxLeftPathSum, maxRightPathSum)
newNode = TreeInfo()
newNode.maxLeftSum = maxLeftPathSum
newNode.maxRightSum = maxRightPathSum
newNode.maxPathSum = maxPathSumFromCurrentNode
return newNode
class TreeInfo:
def __init__(self):
self.maxLeftSum = 0
self.maxRightSum = 0
self.maxPathSum = float('-inf')
# O(N) time and space
def maxPathSum(tree):
# Write your code here.
root = calculateMaxPathSum(tree)
return root.maxPathSum
def calculateMaxPathSum(node):
if not node:
return TreeInfo()
leftSubtree = calculateMaxPathSum(node.left)
rightSubtree = calculateMaxPathSum(node.right)
leftPathSum = leftSubtree.maxPathSumUsingCurrentNode + node.value
rightPathSum = rightSubtree.maxPathSumUsingCurrentNode + node.value
maxPathSumUsingCurrentNode = max(leftPathSum, rightPathSum)
currentMaxPathSum = leftPathSum + rightPathSum - node.value
maxPathSumFromSubtrees = max(leftSubtree.maxPathSum, rightSubtree.maxPathSum)
maxPathSum = max(currentMaxPathSum, maxPathSumFromSubtrees, maxPathSumUsingCurrentNode)
newNode = TreeInfo()
newNode.maxPathSumUsingCurrentNode = maxPathSumUsingCurrentNode
newNode.maxPathSum = maxPathSum
return newNode
class TreeInfo:
def __init__(self):
self.maxPathSumUsingCurrentNode = 0
self.maxPathSum = float('-inf')
# O(logN) space, O(N) time
def maxPathSum(tree):
# Write your code here.
root = calculateMaxPathSum(tree)
return root[1]
def calculateMaxPathSum(node):
if not node:
return [0, float('-inf')]
leftSubtree = calculateMaxPathSum(node.left)
rightSubtree = calculateMaxPathSum(node.right)
leftPathSum = leftSubtree[0] + node.value
rightPathSum = rightSubtree[0] + node.value
maxPathSumUsingCurrentNode = max(leftPathSum, rightPathSum)
currentMaxPathSum = leftPathSum + rightPathSum - node.value
maxPathSumFromSubtrees = max(leftSubtree[1], rightSubtree[1])
maxPathSum = max(currentMaxPathSum, maxPathSumFromSubtrees, maxPathSumUsingCurrentNode)
return [maxPathSumUsingCurrentNode, maxPathSum]