mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update and rename bst_inorder_iterator.py to bt_inorder_iterator.py
This commit is contained in:
parent
d50ed323fd
commit
ef073d11ca
@ -8,29 +8,24 @@ class Node:
|
|||||||
self.left = left
|
self.left = left
|
||||||
self.right = right
|
self.right = right
|
||||||
|
|
||||||
|
|
||||||
class BST_Iterator:
|
class BST_Iterator:
|
||||||
|
|
||||||
def __init__(self, root: Optional[Node]):
|
def __init__(self, root):
|
||||||
|
|
||||||
self.stack = []
|
self.stack = []
|
||||||
self.left_inorder(root)
|
self.left_inorder(root)
|
||||||
|
|
||||||
def left_inorder(self, root):
|
def left_inorder(self, root):
|
||||||
|
|
||||||
while root:
|
while root:
|
||||||
self.stack.append(root)
|
self.stack.append(root)
|
||||||
root = root.left
|
root = root.left
|
||||||
|
|
||||||
def next(self) -> int:
|
def next(self) -> int:
|
||||||
|
|
||||||
top_node = self.stack.pop()
|
top_node = self.stack.pop()
|
||||||
|
|
||||||
if top_node.right:
|
if top_node.right:
|
||||||
self.left_inorder(top_node.right)
|
self.left_inorder(top_node.right)
|
||||||
|
|
||||||
return top_node.val
|
return top_node.val
|
||||||
|
|
||||||
def has_next(self) -> bool:
|
def has_next(self) -> bool:
|
||||||
|
|
||||||
return len(self.stack) > 0
|
return len(self.stack) > 0
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user