Skip to content

Commit 1aa5636

Browse files
author
Partho Biswas
committed
173. Binary Search Tree Iterator
1 parent 7641a77 commit 1aa5636

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ I have solved quite a number of problems from several topics. See the below tabl
251251
|06| [739. Daily Temperatures](https://tinyurl.com/ybbezzmt)| [Python](https://tinyurl.com/wu6rdaw/739_Daily_Temperatures.py)| **[Video 1](https://tinyurl.com/sdbkh3z)**, **[Video 2](https://tinyurl.com/rbvaozl)**, [Art 1](https://tinyurl.com/ss3d9tx) | Medium | 📌 TODO: Check again. A tricky one |
252252
|07| **[150. Evaluate Reverse Polish Notation](https://tinyurl.com/y2xrje8v)** | [Python](https://tinyurl.com/wu6rdaw/150_Evaluate_Reverse_Polish_Notation.py), [Swift](https://tinyurl.com/wuja3c4/150_Evaluate_Reverse_Polish_Notation.swift)| [Vid 1](https://tinyurl.com/s92fp4r) | Medium | --- |
253253
|08| **[341. Flatten Nested List Iterator](https://tinyurl.com/y6saxwjz)** | [Python](https://tinyurl.com/wu6rdaw/341_Flatten_Nested_List_Iterator.py), [Swift](https://tinyurl.com/wuja3c4/341_Flatten_Nested_List_Iterator.swift)| [Vid 1](https://tinyurl.com/vkj4qll), [Art 1](https://tinyurl.com/wnvvafx), [Art 2](https://tinyurl.com/wmrlxmz), [Art 3](https://tinyurl.com/uplntb3), **[Art 4](https://tinyurl.com/v3q9qfw)**, **[Art 5](https://tinyurl.com/rotxca8)** | Medium | TODO: Check again. Very Important. Learned new things |
254+
|09| **[173. Binary Search Tree Iterator](https://tinyurl.com/wp2o8h2)** | [Python](https://tinyurl.com/wu6rdaw/173_Binary_Search_Tree_Iterator.py), [Swift](https://tinyurl.com/wuja3c4/173_Binary_Search_Tree_Iterator.swift)| [Vid 1](https://tinyurl.com/vb62v3q), **[Art 1](https://tinyurl.com/smu9ku3)** | Medium | TODO: Check again. Very Important. Learned new things |
254255

255256
</p>
256257
</details>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
# Approach 1: https://tinyurl.com/smu9ku3
9+
class BSTIterator(object):
10+
11+
def __init__(self, root):
12+
"""
13+
:type root: TreeNode
14+
"""
15+
self.nodesSorted = []
16+
self.nextIndex = 0
17+
self.__inorder(root)
18+
19+
def __inorder(self, root):
20+
if not root:
21+
return
22+
self.__inorder(root.left)
23+
self.nodesSorted.append(root.val)
24+
self.__inorder(root.right)
25+
26+
def next(self):
27+
"""
28+
@return the next smallest number
29+
:rtype: int
30+
"""
31+
value = self.nodesSorted[self.nextIndex]
32+
self.nextIndex += 1
33+
return value
34+
35+
def hasNext(self):
36+
"""
37+
@return whether we have a next smallest number
38+
:rtype: bool
39+
"""
40+
return self.nextIndex < len(self.nodesSorted)
41+
42+
43+
44+
45+
46+
# Approach 2: https://tinyurl.com/smu9ku3
47+
class BSTIterator(object):
48+
49+
def __init__(self, root):
50+
"""
51+
:type root: TreeNode
52+
"""
53+
self.stack = []
54+
self.__leftMostInorder(root)
55+
56+
def __leftMostInorder(self, root):
57+
while root:
58+
self.stack.append(root)
59+
root = root.left
60+
61+
def next(self):
62+
"""
63+
@return the next smallest number
64+
:rtype: int
65+
"""
66+
topMostNode = self.stack.pop()
67+
if topMostNode.right:
68+
self.__leftMostInorder(topMostNode.right)
69+
return topMostNode.val
70+
71+
def hasNext(self):
72+
"""
73+
@return whether we have a next smallest number
74+
:rtype: bool
75+
"""
76+
return len(self.stack) > 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Foundation
2+

0 commit comments

Comments
 (0)