-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathBranch_Sums.py
More file actions
31 lines (23 loc) · 749 Bytes
/
Branch_Sums.py
File metadata and controls
31 lines (23 loc) · 749 Bytes
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
# This is the class of the input root. Do not edit it.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def branchSums(root):
# Write your code here.
sums = []
branchSumsHelper(root, root.value, sums)
return sums
def branchSumsHelper(root, runningSum, sums):
if not root.left and not root.right:
sums.append(runningSum)
return
if root.left:
runningSum += root.left.value
branchSumsHelper(root.left, runningSum, sums)
runningSum -= root.left.value
if root.right:
runningSum += root.right.value
branchSumsHelper(root.right, runningSum, sums)
runningSum -= root.right.value