Skip to content

Commit bc6f5b9

Browse files
committed
update
1 parent 29257c8 commit bc6f5b9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Python/binary-search-tree-iterator.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# Time: O(1)
2+
# Space: O(logn)
3+
#
4+
# Implement an iterator over a binary search tree (BST).
5+
# Your iterator will be initialized with the root node of a BST.
6+
#
7+
# Calling next() will return the next smallest number in the BST.
8+
#
9+
# Note: next() and hasNext() should run in average O(1) time
10+
# and uses O(h) memory, where h is the height of the tree.
11+
#
12+
113
# Definition for a binary tree node
214
class TreeNode:
315
def __init__(self, x):
@@ -30,6 +42,9 @@ def next(self):
3042
if __name__ == "__main__":
3143
root = TreeNode(2)
3244
root.left = TreeNode(1)
45+
46+
# Your BSTIterator will be called like this:
3347
i, v = BSTIterator(root), []
3448
while i.hasNext(): v.append(i.next())
49+
3550
print v

0 commit comments

Comments
 (0)