-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiplySumLevel.py
42 lines (34 loc) · 959 Bytes
/
MultiplySumLevel.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
'''
Created on Jan 8, 2016
@author: Rahul
'''
from Node import *
class MultiplySumLevel:
def getLevelSum(self,root):
cb = CreateBinary()
result = 1
stack = []
stack.append(root)
while stack:
f = False
sum = 0
size = len(stack)
while size > 0 :
node = stack.pop(0)
print node.val
if cb.isLeaf(node):
f = True
sum = sum + node.val
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
size -=1
if f:
result = result * sum
return result
if __name__ == '__main__':
cb = CreateBinary()
root = cb.createTree()
ms= MultiplySumLevel()
print ms.getLevelSum(root)