Skip to content

Commit 07e16a4

Browse files
committed
[Stack] Add a solution to Binary Search Tree Iterator
1 parent aea497c commit 07e16a4

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 310 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 311 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -169,6 +169,7 @@
169169
[Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/)| [Swift](./Stack/TernaryExpressionParser.swift)| Medium| O(n)| O(n)|
170170
[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)| [Swift](./Stack/PreorderTraversal.swift)| Medium| O(n)| O(n)|
171171
[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)| [Swift](./Stack/InorderTraversal.swift)| Medium| O(n)| O(n)|
172+
[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)| [Swift](./Stack/BinarySearchTreeIterator.swift)| Medium| O(n)| O(n)|
172173
[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [Swift](./Stack/PostorderTraversal.swift)| Hard| O(n)| O(n)|
173174
[Decode String](https://leetcode.com/problems/decode-string/)| [Swift](./Stack/DecodeString.swift)| Medium| O(n)| O(n)|
174175

@@ -716,7 +717,7 @@
716717
| [Swift](./String/ReverseWordsStringII.swift) | 186 | [Reverse Words in a String II](https://oj.leetcode.com/problems/reverse-words-in-a-string-ii/) ♥ | Medium |
717718
| [Swift]((./Sort/LargestNumber.swift)) | 179 | [Largest Number](https://oj.leetcode.com/problems/largest-number/) | Medium |
718719
| | 174 | [Dungeon Game](https://oj.leetcode.com/problems/dungeon-game/) | Hard |
719-
| | 173 | [Binary Search Tree Iterator](https://oj.leetcode.com/problems/binary-search-tree-iterator/) | Medium |
720+
| [Swift](./Stack/BinarySearchTreeIterator.swift) | 173 | [Binary Search Tree Iterator](https://oj.leetcode.com/problems/binary-search-tree-iterator/) | Medium |
720721
| [Swift](./Math/FactorialTrailingZeroes.swift) | 172 | [Factorial Trailing Zeroes](https://oj.leetcode.com/problems/factorial-trailing-zeroes/) | Easy |
721722
| [Swift](./Math/ExcelSheetColumnNumber.swift) | 171 | [Excel Sheet Column Number](https://oj.leetcode.com/problems/excel-sheet-column-number/) | Easy |
722723
| [Swift](./Array/TwoSumIII.swift) | 170 | [Two Sum III - Data structure design](https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/) ♥ | Easy |

Stack/BinarySearchTreeIterator.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/binary-search-tree-iterator/
3+
* Primary idea: In order iteration using a stack.
4+
* Time Complexity: O(n), Space Complexity: O(n)
5+
*
6+
* Definition for a binary tree node.
7+
* public class TreeNode {
8+
* public var val: Int
9+
* public var left: TreeNode?
10+
* public var right: TreeNode?
11+
* public init(_ val: Int) {
12+
* self.val = val
13+
* self.left = nil
14+
* self.right = nil
15+
* }
16+
* }
17+
*/
18+
19+
class BSTIterator {
20+
21+
var stack: [TreeNode]
22+
23+
init(_ root: TreeNode?) {
24+
stack = [TreeNode]()
25+
26+
loadAllLeftToStack(from: root)
27+
}
28+
29+
/** @return the next smallest number */
30+
func next() -> Int {
31+
let node = stack.removeLast()
32+
33+
loadAllLeftToStack(from: node.right)
34+
35+
return node.val
36+
}
37+
38+
/** @return whether we have a next smallest number */
39+
func hasNext() -> Bool {
40+
return !stack.isEmpty
41+
}
42+
43+
private func loadAllLeftToStack(from root: TreeNode?) {
44+
var node = root
45+
46+
while let current = node {
47+
stack.append(current)
48+
node = current.left
49+
}
50+
}
51+
}
52+
53+
/**
54+
* Your BSTIterator object will be instantiated and called as such:
55+
* let obj = BSTIterator(root)
56+
* let ret_1: Int = obj.next()
57+
* let ret_2: Bool = obj.hasNext()
58+
*/

0 commit comments

Comments
 (0)