-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaxSumpathtree.py
More file actions
72 lines (59 loc) · 1.84 KB
/
maxSumpathtree.py
File metadata and controls
72 lines (59 loc) · 1.84 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
import sys
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def printPath(root, total):
if total == 0 and root is None:
return True
if root is None:
return False
left = printPath(root.left, total - root.data)
right= False
if not left:
right = printPath(root.right, total - root.data)
if left or right:
print(root.data, end=' ')
return left or right
def getRootToLeafSum(root):
if root is None:
return -sys.maxsize
if root.left is None and root.right is None:
return root.data
left = getRootToLeafSum(root.left)
right = getRootToLeafSum(root.right)
return (left if left > right else right) + root.data
def findMaxSumPath(root):
total = getRootToLeafSum(root)
print('The maximum sum is', total)
print('The maximum sum path is ', end='')
printPath(root, total)
if __name__ == '__main__':
root = None
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(8)
root.left.right = Node(4)
root.right.left = Node(5)
root.right.right = Node(6)
root.left.right.left = Node(10)
root.right.left.left = Node(7)
root.right.left.right = Node(9)
root.right.right.right = Node(5)
findMaxSumPath(root)
# def _recurse_tree(parent, depth, source):
# last_line = source.readline().rstrip()
# while last_line:
# tabs = last_line.count('\t')
# if tabs < depth:
# break
# node = last_line.strip()
# if tabs >= depth:
# if parent is not None:
# print "%s: %s" %(parent, node)
# last_line = _recurse_tree(node, tabs+1, source)
# return last_line
# inFile = open("triangle.txt")
# _recurse_tree(None, 0, inFile)