-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path173-binary-search-tree-iterator.py
36 lines (31 loc) · 1.07 KB
/
173-binary-search-tree-iterator.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
# 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
class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = []
self.node = root
def next(self) -> int:
while self.stack or self.node:
if self.node:
self.stack.append(self.node)
self.node = self.node.left
else:
self.node = self.stack.pop()
val = self.node.val
self.node = self.node.right
return val
def hasNext(self) -> bool:
if self.stack or self.node:
return True
return False
# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()
# time O(1), due to amortized
# space O(h), only store left nodes, so stack's size is tree height
# using tree and dfs (inorder and iterative) and stack to store the left nodes