-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsum-root-to-leaf-numbers.py
60 lines (51 loc) · 1.81 KB
/
sum-root-to-leaf-numbers.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 129. Sum Root to Leaf Numbers
# 🟠 Medium
#
# https://leetcode.com/problems/sum-root-to-leaf-numbers/
#
# Tags: Tree - Depth-First Search - Binary Tree
import timeit
from typing import Optional
from utils.binary_tree import TreeNode
# Traverse the tree using preorder DFS, add the value of the current
# node to the path sum, if the node is a leaf, return that value,
# otherwise call the function for any non-null children.
#
# Time complexity: O(n) - We will visit every node in the tree and do
# constant work for each.
# Space complexity: O(h) - The height of the call stack will be the
# height of the tree, worst O(n), best O(log(n)).
#
# Runtime 28 ms Beats 88.93%
# Memory 13.8 MB Beats 95.89%
class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
def dfs(node: TreeNode, path: int) -> int:
path *= 10
path += node.val
if not node.left and not node.right:
return path
return (dfs(node.left, path) if node.left else 0) + (
dfs(node.right, path) if node.right else 0
)
return dfs(root, 0)
def test():
executors = [Solution]
tests = []
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.methodCall(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()