Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

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))

262

Explanation: 124 + 125 + 13 = 262