Skip to content

Commit 0cd9433

Browse files
author
He Hao
committed
add solution to 173
1 parent e894364 commit 0cd9433

File tree

1 file changed

+29
-0
lines changed
  • leetcode_python_solutions

1 file changed

+29
-0
lines changed

leetcode_python_solutions/173.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class BSTIterator:
8+
9+
def __init__(self, root: Optional[TreeNode]):
10+
self.stack = []
11+
self.traverse_left(root)
12+
13+
def traverse_left(self, node):
14+
while node:
15+
self.stack.append(node)
16+
node = node.left
17+
18+
def next(self) -> int:
19+
node = self.stack.pop()
20+
self.traverse_left(node.right)
21+
return node.val
22+
23+
def hasNext(self) -> bool:
24+
return len(self.stack) > 0
25+
26+
# Your BSTIterator object will be instantiated and called as such:
27+
# obj = BSTIterator(root)
28+
# param_1 = obj.next()
29+
# param_2 = obj.hasNext()

0 commit comments

Comments
 (0)