We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 29257c8 commit bc6f5b9Copy full SHA for bc6f5b9
Python/binary-search-tree-iterator.py
@@ -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
+
13
# Definition for a binary tree node
14
class TreeNode:
15
def __init__(self, x):
@@ -30,6 +42,9 @@ def next(self):
30
42
if __name__ == "__main__":
31
43
root = TreeNode(2)
32
44
root.left = TreeNode(1)
45
46
+ # Your BSTIterator will be called like this:
33
47
i, v = BSTIterator(root), []
34
48
while i.hasNext(): v.append(i.next())
49
35
50
print v
0 commit comments