-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbinary-search-tree-iterator.py
More file actions
102 lines (87 loc) · 3.07 KB
/
binary-search-tree-iterator.py
File metadata and controls
102 lines (87 loc) · 3.07 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 173. Binary Search Tree Iterator
# 🟠 Medium
#
# https://leetcode.com/problems/binary-search-tree-iterator/
#
# Tags: Stack - Tree - Design - Binary Search Tree - Binary Tree - Iterator
import timeit
from collections import deque
from typing import List, Optional
from data import deserializeStringArrayToBinaryTree
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# If we are not asked to maintain the BST structure internally, and are
# ever only going to be asked to iterate over the tree, there is no
# reason to. The easiest way is to get a list of the tree values inorder
# and pop elements when the next method is called.
#
# Time complexity: O(n) - for the init method, O(1) for the next and
# hasNext methods.
# Space complexity: O(n) - We keep all elements in memory.
#
# Runtime: 107 ms, faster than 61.24%
# Memory Usage: 20.1 MB, less than 70.78%
class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
# Store the values of the tree in inorder using a deque. If
# we couldn't use a deque it would be the same to store the
# elements reversed in a list and pop from the right.
self.nums = deque(self.getValues(root))
# Get the values of a tree inorder.
def getValues(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
return (
self.getValues(root.left) + [root.val] + self.getValues(root.right)
)
def next(self) -> int:
return self.nums.popleft()
def hasNext(self) -> bool:
return len(self.nums) > 0
# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()
def test():
executors = [BSTIterator]
tests = [
[
"[7,3,15,null,null,9,20]",
[
"next",
"next",
"hasNext",
"next",
"hasNext",
"next",
"hasNext",
"next",
"hasNext",
],
[3, 7, True, 9, True, 15, True, 20, False],
],
["[]", ["hasNext"], [False]],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(int(float("1"))):
for i, t in enumerate(tests):
root = deserializeStringArrayToBinaryTree(t[0])
sol = executor(root)
for i, call in enumerate(t[1]):
result = getattr(sol, call)()
exp = t[2][i]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for "
+ f"test {i} 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()