Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9797127

Browse files
committedFeb 1, 2025
Add solution #173
1 parent 6d40d4a commit 9797127

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
168|[Excel Sheet Column Title](./0168-excel-sheet-column-title.js)|Easy|
165165
169|[Majority Element](./0169-majority-element.js)|Easy|
166166
171|[Excel Sheet Column Number](./0171-excel-sheet-column-number.js)|Easy|
167+
173|[Binary Search Tree Iterator](./0173-binary-search-tree-iterator.js)|Medium|
167168
179|[Largest Number](./0179-largest-number.js)|Medium|
168169
187|[Repeated DNA Sequences](./0187-repeated-dna-sequences.js)|Medium|
169170
189|[Rotate Array](./0189-rotate-array.js)|Medium|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* 173. Binary Search Tree Iterator
3+
* https://leetcode.com/problems/binary-search-tree-iterator/
4+
* Difficulty: Medium
5+
*
6+
* Implement the BSTIterator class that represents an iterator over the in-order traversal
7+
* of a binary search tree (BST):
8+
* - BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of
9+
* the BST is given as part of the constructor. The pointer should be initialized to a
10+
* non-existent number smaller than any element in the BST.
11+
* - boolean hasNext() Returns true if there exists a number in the traversal to the right
12+
* of the pointer, otherwise returns false.
13+
* - int next() Moves the pointer to the right, then returns the number at the pointer.
14+
*
15+
* Notice that by initializing the pointer to a non-existent smallest number, the first call
16+
* to next() will return the smallest element in the BST.
17+
*
18+
* You may assume that next() calls will always be valid. That is, there will be at least a
19+
* next number in the in-order traversal when next() is called.
20+
*/
21+
22+
/**
23+
* Definition for a binary tree node.
24+
* function TreeNode(val, left, right) {
25+
* this.val = (val===undefined ? 0 : val)
26+
* this.left = (left===undefined ? null : left)
27+
* this.right = (right===undefined ? null : right)
28+
* }
29+
*/
30+
/**
31+
* @param {TreeNode} root
32+
*/
33+
var BSTIterator = function(root) {
34+
this.stack = [];
35+
this.root = root;
36+
};
37+
38+
/**
39+
* @return {number}
40+
*/
41+
BSTIterator.prototype.next = function() {
42+
while (this.root) {
43+
this.stack.push(this.root);
44+
this.root = this.root.left;
45+
}
46+
this.root = this.stack.pop();
47+
48+
const result = this.root.val;
49+
this.root = this.root.right;
50+
return result;
51+
};
52+
53+
/**
54+
* @return {boolean}
55+
*/
56+
BSTIterator.prototype.hasNext = function() {
57+
return this.root || this.stack.length;
58+
};

0 commit comments

Comments
 (0)
Please sign in to comment.