-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinaryTreeMaximumPathSum_v1.py
More file actions
99 lines (72 loc) · 1.74 KB
/
BinaryTreeMaximumPathSum_v1.py
File metadata and controls
99 lines (72 loc) · 1.74 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# encoding: utf-8
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
d = {}
def cache(f):
def _(inst, root):
ids = id(root)
rs = d.get(ids)
if rs is None:
rs = f(inst, root)
d[ids] = rs
return rs
return _
class Solution:
# @param {TreeNode} root
# @return {integer}
def maxPathSum(self, root):
d = {}
if not root:
return 0
queue = [root]
maxl = -2**31
while queue:
p = queue.pop()
ld = rd = 0
if p.left:
ld = self.get_max_length(p.left)
queue = [p.left] + queue
if p.right:
rd = self.get_max_length(p.right)
queue = [p.right] + queue
v = max(ld, 0) + max(rd, 0) + p.val
if maxl < v:
maxl = v
return maxl
@cache
def get_max_length(self, root):
if not root:
return 0
l = self.get_max_length(root.left)
r = self.get_max_length(root.right)
return max(l, r, 0)+root.val
if __name__ == '__main__':
s = Solution()
n1 = TreeNode(1)
n2 = TreeNode(2)
n3 = TreeNode(3)
r = n1
n1.left = n2
n1.right = n3
#print s.get_max_length(r)
print s.maxPathSum(r)
r = TreeNode(-3)
print s.maxPathSum(r)
n1 = TreeNode(2)
n2 = TreeNode(-1)
r = n1
n1.left = n2
print s.maxPathSum(r)
n1 = TreeNode(0)
n2 = TreeNode(1)
n3 = TreeNode(1)
r = n1
n1.left = n2
n1.right = n3
print s.maxPathSum(r)
print s.get_max_length(r.left)