Skip to content

Commit 29257c8

Browse files
committed
update
1 parent 6fd29b8 commit 29257c8

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Python/binary-search-tree-iterator.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for a binary tree node
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
class BSTIterator:
9+
# @param root, a binary search tree's root node
10+
def __init__(self, root):
11+
self.stack = []
12+
self.cur = root
13+
14+
# @return a boolean, whether we have a next smallest number
15+
def hasNext(self):
16+
return self.stack or self.cur
17+
18+
# @return an integer, the next smallest number
19+
def next(self):
20+
while self.cur:
21+
self.stack.append(self.cur)
22+
self.cur = self.cur.left
23+
24+
self.cur = self.stack.pop()
25+
node = self.cur
26+
self.cur = self.cur.right
27+
28+
return node.val
29+
30+
if __name__ == "__main__":
31+
root = TreeNode(2)
32+
root.left = TreeNode(1)
33+
i, v = BSTIterator(root), []
34+
while i.hasNext(): v.append(i.next())
35+
print v

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
LeetCode
22
========
33

4-
Up to date (2014-12-30), there are total `172` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
4+
Up to date (2014-12-31), there are total `173` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
55
The number of problems is increasing recently.
6-
Here is the classification of all `172` problems.
6+
Here is the classification of all `173` problems.
77
I'll keep updating for full summary and better solutions. Stay tuned for updates.
88

99
---
@@ -204,13 +204,16 @@ Problem | Solution | Time | Space | Difficul
204204
##Stack
205205
Problem | Solution | Time | Space | Difficulty | Notes
206206
--------------- | --------------- | --------------- | --------------- | -------------- | -----
207+
[Binary Search Tree Iterator] | [binary-search-tree-iterator.py] | _O(1)_| _O(logn)_| Medium
207208
[Evaluate Reverse Polish Notation]| [evaluate-reverse-polish-notation.py]| _O(n)_| _O(n)_| Medium |
208209
[Longest Valid Parentheses]| [longest-valid-parentheses.py] | _O(n)_ | _O(1)_ | Hard |
209210
[Min Stack] | [min-stack.py] | _O(n)_ | _O(1)_ | Easy |
210211
[Simplify Path]| [simplify-path.py] | _O(n)_ | _O(n)_ | Medium |
211212
[Symmetric Tree]| [symmetric-tree.py] | _O(n)_ | _O(logn)_ | Easy |
212213
[Valid Parentheses]| [valid-parentheses.py] | _O(n)_ | _O(n)_ | Easy |
213214

215+
[Binary Search Tree Iterator]:https://oj.leetcode.com/problems/binary-search-tree-iterator/
216+
[binary-search-tree-iterator.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/binary-search-tree-iterator.py
214217
[Evaluate Reverse Polish Notation]:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
215218
[evaluate-reverse-polish-notation.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/evaluate-reverse-polish-notation.py
216219
[Longest Valid Parentheses]:https://oj.leetcode.com/problems/longest-valid-parentheses/

0 commit comments

Comments
 (0)