Skip to content

Commit 6898c38

Browse files
committed
binary search tree iterator
1 parent 9d43309 commit 6898c38

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

173.BinarySearchTreeIterator.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
173. Binary Search Tree Iterator
3+
Medium
4+
Stack | Tree | Design | Binary Search Tree | Binary Tree | Iterator
5+
---
6+
Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):
7+
8+
BSTIterator(TreeNode root) Initializes an object of the BSTIterator class.
9+
The root of the BST is given as part of the constructor.
10+
The pointer should be initialized to a non-existent number smaller than any element in the BST.
11+
boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer,
12+
otherwise returns false.
13+
int next() Moves the pointer to the right, then returns the number at the pointer.
14+
Notice that by initializing the pointer to a non-existent smallest number,
15+
the first call to next() will return the smallest element in the BST.
16+
17+
You may assume that next() calls will always be valid. That is,
18+
there will be at least a next number in the in-order traversal when next() is called.
19+
20+
Example 1:
21+
Input
22+
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
23+
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
24+
Output
25+
[null, 3, 7, true, 9, true, 15, true, 20, false]
26+
27+
Explanation
28+
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
29+
bSTIterator.next(); // return 3
30+
bSTIterator.next(); // return 7
31+
bSTIterator.hasNext(); // return True
32+
bSTIterator.next(); // return 9
33+
bSTIterator.hasNext(); // return True
34+
bSTIterator.next(); // return 15
35+
bSTIterator.hasNext(); // return True
36+
bSTIterator.next(); // return 20
37+
bSTIterator.hasNext(); // return False
38+
39+
Constraints:
40+
The number of nodes in the tree is in the range [1, 105].
41+
0 <= Node.val <= 106
42+
At most 105 calls will be made to hasNext, and next.
43+
44+
Follow up:
45+
Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory,
46+
where h is the height of the tree?
47+
"""
48+
49+
from typing import Optional
50+
51+
52+
# Definition for a binary tree node.
53+
class TreeNode:
54+
def __init__(self, val=0, left=None, right=None):
55+
self.val = val
56+
self.left = left
57+
self.right = right
58+
59+
60+
class BSTIterator:
61+
62+
def __init__(self, root: Optional[TreeNode]):
63+
self.stack = []
64+
while root:
65+
self.stack.append(root)
66+
root = root.left
67+
68+
def next(self) -> int:
69+
popped = self.stack.pop()
70+
curr = popped.right
71+
while curr:
72+
self.stack.append(curr)
73+
curr = curr.left
74+
return popped.val
75+
76+
def hasNext(self) -> bool:
77+
return self.stack != []
78+
79+
80+
# Your BSTIterator object will be instantiated and called as such:
81+
# obj = BSTIterator(root)
82+
# param_1 = obj.next()
83+
# param_2 = obj.hasNext()

0 commit comments

Comments
 (0)