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 Uber:

Given a binary tree, and a target number, find if there is a path from the root to any leaf that sums up to the target.

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 target_sum_bst(root, target):

Fill this in.

1

/ \

2 3

\ \

6 4

n6 = Node(6) n4 = Node(4) n3 = Node(3, None, n4) n2 = Node(2, None, n6) n1 = Node(1, n2, n3)

print(target_sum_bst(n1, 9))

True

Path from 1 -> 2 -> 6