Hi, here’s your problem today. This problem was recently asked by Amazon:
A number can be constructed by a path from the root to a leaf. Given a binary tree, sum all the numbers that can be constructed from the root to all leaves.
Here’s an example and some starter code.
class Node: def init(self, value, left=None, right=None): self.value = value self.left = left self.right = right
def repr(self): return f”({self.value}, {self.left}, {self.right})”
def bst_numbers_sum(root, num=0):
Fill this in.
n5 = Node(5) n4 = Node(4) n3 = Node(3) n2 = Node(2, n4, n5) n1 = Node(1, n2, n3)
1
/ \
2 3
/ \
4 5
print(bst_numbers_sum(n1))