-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_tree_iterator.py
89 lines (73 loc) · 1.96 KB
/
binary_search_tree_iterator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Time: O(1)
# Space: O(h), h is height of binary tree
#
# Implement an iterator over a binary search tree (BST).
# Your iterator will be initialized with the root node of a BST.
#
# Calling next() will return the next smallest number in the BST.
#
# Note: next() and hasNext() should run in average O(1) time
# and uses O(h) memory, where h is the height of the tree.
#
# Definition for a binary tree node
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class EmptyStackError(Exception):
pass
class Stack(object):
def __init__(self):
self.stack = []
self.top = 0
def push(self, item):
self.stack.append(item)
self.top += 1
def pop(self):
if self.top == 0:
raise EmptyStackError()
item = self.stack.pop()
self.top -= 1
return item
def is_emtpy(self):
if self.top:
return False
return True
class BSTIterator(object):
"""
利用stack记录遍历时的节点,典型的用空间换时间的算法。
"""
def __init__(self, root):
"""
:type root: TreeNode
"""
self.root = root
# 用栈记录遍历的路径节点
self.iter_node_stack = Stack()
self._iter(root)
def hasNext(self):
"""
:rtype: bool
"""
if self.iter_node_stack.is_emtpy():
return False
return True
def next(self):
"""
:rtype: int
"""
node = self.iter_node_stack.pop()
if node.right:
self._iter(node.right)
return node.val
def _iter(self, node):
"""
把遍历左子树的路径记录下来
"""
while node:
self.iter_node_stack.push(node)
node = node.left
# Your BSTIterator will be called like this:
# i, v = BSTIterator(root), []
# while i.hasNext(): v.append(i.next())