Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95e4036

Browse files
committed
add js solution to leetcode problem: no.0173
1 parent 95d669b commit 95e4036

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed

‎solution/0100-0199/0173.Binary Search Tree Iterator/README.md‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
<p><strong>示例:</strong></p>
2727
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0173.Binary%20Search%20Tree%20Iterator/images/bst-tree.png" style="width: 189px; height: 178px;" />
28+
<div>
2829
<pre>
2930
<strong>输入</strong>
3031
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
@@ -44,7 +45,7 @@ bSTIterator.hasNext(); // 返回 True
4445
bSTIterator.next(); // 返回 20
4546
bSTIterator.hasNext(); // 返回 False
4647
</pre>
47-
48+
</div>
4849
<p> </p>
4950

5051
<p><strong>提示:</strong></p>
@@ -171,6 +172,56 @@ class BSTIterator {
171172
*/
172173
```
173174

175+
## **JavaScript**
176+
177+
```js
178+
/**
179+
* Definition for a binary tree node.
180+
* function TreeNode(val, left, right) {
181+
* this.val = (val===undefined ? 0 : val)
182+
* this.left = (left===undefined ? null : left)
183+
* this.right = (right===undefined ? null : right)
184+
* }
185+
*/
186+
/**
187+
* @param {TreeNode} root
188+
*/
189+
var BSTIterator = function (root) {
190+
this.stk = [];
191+
this.cur = root;
192+
}
193+
194+
195+
/**
196+
* @return {number}
197+
*/
198+
BSTIterator.prototype.next = function () {
199+
while (this.cur) {
200+
this.stk.push(this.cur);
201+
this.cur = this.cur.left;
202+
}
203+
this.cur = this.stk.pop();
204+
let res = this.cur.val;
205+
this.cur = this.cur.right;
206+
return (res);
207+
};
208+
209+
/**
210+
* @return {boolean}
211+
*/
212+
BSTIterator.prototype.hasNext = function () {
213+
if (this.stk.length === 0 && this.cur === null) return false;
214+
return true;
215+
};
216+
217+
/**
218+
* Your BSTIterator object will be instantiated and called as such:
219+
* var obj = new BSTIterator(root)
220+
* var param_1 = obj.next()
221+
* var param_2 = obj.hasNext()
222+
*/
223+
```
224+
174225
### **...**
175226

176227
```

‎solution/0100-0199/0173.Binary Search Tree Iterator/README_EN.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<p>&nbsp;</p>
2020
<p><strong>Example 1:</strong></p>
2121
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0173.Binary%20Search%20Tree%20Iterator/images/bst-tree.png" style="width: 189px; height: 178px;" />
22+
<div>
2223
<pre>
2324
<strong>Input</strong>
2425
[&quot;BSTIterator&quot;, &quot;next&quot;, &quot;next&quot;, &quot;hasNext&quot;, &quot;next&quot;, &quot;hasNext&quot;, &quot;next&quot;, &quot;hasNext&quot;, &quot;next&quot;, &quot;hasNext&quot;]
@@ -38,6 +39,7 @@ bSTIterator.hasNext(); // return True
3839
bSTIterator.next(); // return 20
3940
bSTIterator.hasNext(); // return False
4041
</pre>
42+
</div>
4143

4244
<p>&nbsp;</p>
4345
<p><strong>Constraints:</strong></p>
@@ -153,6 +155,56 @@ class BSTIterator {
153155
*/
154156
```
155157

158+
## **JavaScript**
159+
160+
```js
161+
/**
162+
* Definition for a binary tree node.
163+
* function TreeNode(val, left, right) {
164+
* this.val = (val===undefined ? 0 : val)
165+
* this.left = (left===undefined ? null : left)
166+
* this.right = (right===undefined ? null : right)
167+
* }
168+
*/
169+
/**
170+
* @param {TreeNode} root
171+
*/
172+
var BSTIterator = function (root) {
173+
this.stk = [];
174+
this.cur = root;
175+
}
176+
177+
178+
/**
179+
* @return {number}
180+
*/
181+
BSTIterator.prototype.next = function () {
182+
while (this.cur) {
183+
this.stk.push(this.cur);
184+
this.cur = this.cur.left;
185+
}
186+
this.cur = this.stk.pop();
187+
let res = this.cur.val;
188+
this.cur = this.cur.right;
189+
return (res);
190+
};
191+
192+
/**
193+
* @return {boolean}
194+
*/
195+
BSTIterator.prototype.hasNext = function () {
196+
if (this.stk.length === 0 && this.cur === null) return false;
197+
return true;
198+
};
199+
200+
/**
201+
* Your BSTIterator object will be instantiated and called as such:
202+
* var obj = new BSTIterator(root)
203+
* var param_1 = obj.next()
204+
* var param_2 = obj.hasNext()
205+
*/
206+
```
207+
156208
### **...**
157209

158210
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
*/
12+
var BSTIterator = function (root) {
13+
this.stk = [];
14+
this.cur = root;
15+
}
16+
17+
18+
/**
19+
* @return {number}
20+
*/
21+
BSTIterator.prototype.next = function () {
22+
while (this.cur) {
23+
this.stk.push(this.cur);
24+
this.cur = this.cur.left;
25+
}
26+
this.cur = this.stk.pop();
27+
let res = this.cur.val;
28+
this.cur = this.cur.right;
29+
return (res);
30+
};
31+
32+
/**
33+
* @return {boolean}
34+
*/
35+
BSTIterator.prototype.hasNext = function () {
36+
if (this.stk.length === 0 && this.cur === null) return false;
37+
return true;
38+
};
39+
40+
/**
41+
* Your BSTIterator object will be instantiated and called as such:
42+
* var obj = new BSTIterator(root)
43+
* var param_1 = obj.next()
44+
* var param_2 = obj.hasNext()
45+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /