Skip to content

Commit bb6809e

Browse files
authored
Update binary-search-tree-iterator.py
1 parent 87419d6 commit bb6809e

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

Python/binary-search-tree-iterator.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@ def __init__(self, x):
1111
class BSTIterator(object):
1212
# @param root, a binary search tree's root node
1313
def __init__(self, root):
14-
self.stack = []
15-
self.cur = root
14+
self.__stk = []
15+
self.__traversalLeft(root)
1616

1717
# @return a boolean, whether we have a next smallest number
1818
def hasNext(self):
19-
return self.stack or self.cur
19+
return self.__stk
2020

2121
# @return an integer, the next smallest number
2222
def next(self):
23-
while self.cur:
24-
self.stack.append(self.cur)
25-
self.cur = self.cur.left
26-
27-
self.cur = self.stack.pop()
28-
node = self.cur
29-
self.cur = self.cur.right
30-
23+
node = self.__stk.pop()
24+
self.__traversalLeft(node.right)
3125
return node.val
26+
27+
def __traversalLeft(self, node):
28+
while node is not None:
29+
self.__stk.append(node)
30+
node = node.left
3231

0 commit comments

Comments
 (0)