-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path124 Binary Tree Maximum Path Sum.py
69 lines (52 loc) · 1.85 KB
/
124 Binary Tree Maximum Path Sum.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
"""
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
Author: Rajeev Ranjan
"""
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
global_max = -1<<31
def maxPathSum(self, root):
"""
:param root: TreeNode
:return: integer
"""
self.get_max_component(root)
# global_max can in ANY path in the tree
return self.global_max
def get_max_component(self, root):
"""
Algorithm:
dfs
The path may start and end at any node in the tree.
parent
/
cur
/ \
L R
at current: the candidate max (final result) is cur+L+R
at current: the max component (intermediate result) is cur or cur+L or cur+R
Reference: http://fisherlei.blogspot.sg/2013/01/leetcode-binary-tree-maximum-path-sum.html
:param root:
:return:
"""
if not root:
return 0
left_max_component = self.get_max_component(root.left)
right_max_component = self.get_max_component(root.right)
# update global max
current_max_sum = max(root.val, root.val+left_max_component, root.val+right_max_component, root.val+left_max_component+right_max_component) # four situations
self.global_max = max(self.global_max, current_max_sum)
# return value for upper layer to calculate the current_max_sum
return max(root.val, root.val+left_max_component, root.val+right_max_component) # excluding arch (i.e. root.val+left_max_component+right_max_component)