forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0173.py
More file actions
28 lines (25 loc) · 662 Bytes
/
Copy path0173.py
File metadata and controls
28 lines (25 loc) · 662 Bytes
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
class BSTIterator:
def __init__(self, root):
"""
:type root: TreeNode
"""
self.stack = list()
self.pushLeft(root)
def next(self):
"""
@return the next smallest number
:rtype: int
"""
node = self.stack.pop()
self.pushLeft(node.right)
return node.val
def pushLeft(self, node):
while node:
self.stack.append(node)
node = node.left
def hasNext(self):
"""
@return whether we have a next smallest number
:rtype: bool
"""
return True if self.stack else False