-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinaryTreeMaximumPathSum_v0.py
More file actions
61 lines (45 loc) · 1.18 KB
/
BinaryTreeMaximumPathSum_v0.py
File metadata and controls
61 lines (45 loc) · 1.18 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
#!/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
class Solution:
# @param {TreeNode} root
# @return {integer}
def maxPathSum(self, root):
if not root:
return 0
queue = [root]
maxl = 0
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 = ld + rd + p.val
if maxl < v:
maxl = v
return maxl
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)+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)