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 6c1565a

Browse files
feat: add solutions to lc problem: No.1586 (#1921)
No.1586.Binary Search Tree Iterator II
1 parent acf1db0 commit 6c1565a

File tree

7 files changed

+842
-0
lines changed

7 files changed

+842
-0
lines changed

‎solution/1500-1599/1586.Binary Search Tree Iterator II/README.md‎

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,309 @@ bSTIterator.prev(); // 状态变为 [3, 7, <u>9</u>, 15, 20], 返回
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68+
**方法一:中序遍历 + 数组**
69+
70+
我们可以使用中序遍历将二叉搜索树的所有节点的值存储到数组 $nums$ 中,然后使用数组实现迭代器。我们定义一个指针 $i,ドル初始时 $i = -1,ドル表示指向数组 $nums$ 中的一个元素。每次调用 $next()$ 时,我们将 $i$ 的值加 1ドル,ドル并返回 $nums[i]$;每次调用 $prev()$ 时,我们将 $i$ 的值减 1ドル,ドル并返回 $nums[i]$。
71+
72+
时间复杂度方面,初始化迭代器需要 $O(n)$ 的时间,其中 $n$ 是二叉搜索树的节点数。每次调用 $next()$ 和 $prev()$ 都需要 $O(1)$ 的时间。空间复杂度方面,我们需要 $O(n)$ 的空间存储二叉搜索树的所有节点的值。
73+
6874
<!-- tabs:start -->
6975

7076
### **Python3**
7177

7278
<!-- 这里可写当前语言的特殊实现逻辑 -->
7379

7480
```python
81+
# Definition for a binary tree node.
82+
# class TreeNode:
83+
# def __init__(self, val=0, left=None, right=None):
84+
# self.val = val
85+
# self.left = left
86+
# self.right = right
87+
class BSTIterator:
88+
def __init__(self, root: Optional[TreeNode]):
89+
self.nums = []
90+
91+
def dfs(root):
92+
if root is None:
93+
return
94+
dfs(root.left)
95+
self.nums.append(root.val)
96+
dfs(root.right)
97+
98+
dfs(root)
99+
self.i = -1
100+
101+
def hasNext(self) -> bool:
102+
return self.i < len(self.nums) - 1
75103

104+
def next(self) -> int:
105+
self.i += 1
106+
return self.nums[self.i]
107+
108+
def hasPrev(self) -> bool:
109+
return self.i > 0
110+
111+
def prev(self) -> int:
112+
self.i -= 1
113+
return self.nums[self.i]
114+
115+
116+
# Your BSTIterator object will be instantiated and called as such:
117+
# obj = BSTIterator(root)
118+
# param_1 = obj.hasNext()
119+
# param_2 = obj.next()
120+
# param_3 = obj.hasPrev()
121+
# param_4 = obj.prev()
76122
```
77123

78124
### **Java**
79125

80126
<!-- 这里可写当前语言的特殊实现逻辑 -->
81127

82128
```java
129+
/**
130+
* Definition for a binary tree node.
131+
* public class TreeNode {
132+
* int val;
133+
* TreeNode left;
134+
* TreeNode right;
135+
* TreeNode() {}
136+
* TreeNode(int val) { this.val = val; }
137+
* TreeNode(int val, TreeNode left, TreeNode right) {
138+
* this.val = val;
139+
* this.left = left;
140+
* this.right = right;
141+
* }
142+
* }
143+
*/
144+
class BSTIterator {
145+
private List<Integer> nums = new ArrayList<>();
146+
private int i = -1;
147+
148+
public BSTIterator(TreeNode root) {
149+
dfs(root);
150+
}
151+
152+
public boolean hasNext() {
153+
return i < nums.size() - 1;
154+
}
155+
156+
public int next() {
157+
return nums.get(++i);
158+
}
159+
160+
public boolean hasPrev() {
161+
return i > 0;
162+
}
163+
164+
public int prev() {
165+
return nums.get(--i);
166+
}
167+
168+
private void dfs(TreeNode root) {
169+
if (root == null) {
170+
return;
171+
}
172+
dfs(root.left);
173+
nums.add(root.val);
174+
dfs(root.right);
175+
}
176+
}
177+
178+
/**
179+
* Your BSTIterator object will be instantiated and called as such:
180+
* BSTIterator obj = new BSTIterator(root);
181+
* boolean param_1 = obj.hasNext();
182+
* int param_2 = obj.next();
183+
* boolean param_3 = obj.hasPrev();
184+
* int param_4 = obj.prev();
185+
*/
186+
```
187+
188+
### **C++**
189+
190+
```cpp
191+
/**
192+
* Definition for a binary tree node.
193+
* struct TreeNode {
194+
* int val;
195+
* TreeNode *left;
196+
* TreeNode *right;
197+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
198+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
199+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
200+
* };
201+
*/
202+
class BSTIterator {
203+
public:
204+
BSTIterator(TreeNode* root) {
205+
dfs(root);
206+
n = nums.size();
207+
}
208+
209+
bool hasNext() {
210+
return i < n - 1;
211+
}
212+
213+
int next() {
214+
return nums[++i];
215+
}
216+
217+
bool hasPrev() {
218+
return i > 0;
219+
}
220+
221+
int prev() {
222+
return nums[--i];
223+
}
224+
225+
private:
226+
vector<int> nums;
227+
int i = -1;
228+
int n;
229+
230+
void dfs(TreeNode* root) {
231+
if (!root) {
232+
return;
233+
}
234+
dfs(root->left);
235+
nums.push_back(root->val);
236+
dfs(root->right);
237+
}
238+
};
239+
240+
/**
241+
* Your BSTIterator object will be instantiated and called as such:
242+
* BSTIterator* obj = new BSTIterator(root);
243+
* bool param_1 = obj->hasNext();
244+
* int param_2 = obj->next();
245+
* bool param_3 = obj->hasPrev();
246+
* int param_4 = obj->prev();
247+
*/
248+
```
249+
250+
### **Go**
251+
252+
```go
253+
/**
254+
* Definition for a binary tree node.
255+
* type TreeNode struct {
256+
* Val int
257+
* Left *TreeNode
258+
* Right *TreeNode
259+
* }
260+
*/
261+
type BSTIterator struct {
262+
nums []int
263+
i, n int
264+
}
265+
266+
func Constructor(root *TreeNode) BSTIterator {
267+
nums := []int{}
268+
var dfs func(*TreeNode)
269+
dfs = func(root *TreeNode) {
270+
if root == nil {
271+
return
272+
}
273+
dfs(root.Left)
274+
nums = append(nums, root.Val)
275+
dfs(root.Right)
276+
}
277+
dfs(root)
278+
return BSTIterator{nums, -1, len(nums)}
279+
}
280+
281+
func (this *BSTIterator) HasNext() bool {
282+
return this.i < this.n-1
283+
}
284+
285+
func (this *BSTIterator) Next() int {
286+
this.i++
287+
return this.nums[this.i]
288+
}
289+
290+
func (this *BSTIterator) HasPrev() bool {
291+
return this.i > 0
292+
}
293+
294+
func (this *BSTIterator) Prev() int {
295+
this.i--
296+
return this.nums[this.i]
297+
}
298+
299+
/**
300+
* Your BSTIterator object will be instantiated and called as such:
301+
* obj := Constructor(root);
302+
* param_1 := obj.HasNext();
303+
* param_2 := obj.Next();
304+
* param_3 := obj.HasPrev();
305+
* param_4 := obj.Prev();
306+
*/
307+
```
308+
309+
### **TypeScript**
310+
311+
```ts
312+
/**
313+
* Definition for a binary tree node.
314+
* class TreeNode {
315+
* val: number
316+
* left: TreeNode | null
317+
* right: TreeNode | null
318+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
319+
* this.val = (val===undefined ? 0 : val)
320+
* this.left = (left===undefined ? null : left)
321+
* this.right = (right===undefined ? null : right)
322+
* }
323+
* }
324+
*/
325+
326+
class BSTIterator {
327+
private nums: number[];
328+
private n: number;
329+
private i: number;
330+
331+
constructor(root: TreeNode | null) {
332+
this.nums = [];
333+
const dfs = (root: TreeNode | null) => {
334+
if (!root) {
335+
return;
336+
}
337+
dfs(root.left);
338+
this.nums.push(root.val);
339+
dfs(root.right);
340+
};
341+
dfs(root);
342+
this.n = this.nums.length;
343+
this.i = -1;
344+
}
345+
346+
hasNext(): boolean {
347+
return this.i < this.n - 1;
348+
}
349+
350+
next(): number {
351+
return this.nums[++this.i];
352+
}
353+
354+
hasPrev(): boolean {
355+
return this.i > 0;
356+
}
357+
358+
prev(): number {
359+
return this.nums[--this.i];
360+
}
361+
}
83362

363+
/**
364+
* Your BSTIterator object will be instantiated and called as such:
365+
* var obj = new BSTIterator(root)
366+
* var param_1 = obj.hasNext()
367+
* var param_2 = obj.next()
368+
* var param_3 = obj.hasPrev()
369+
* var param_4 = obj.prev()
370+
*/
84371
```
85372

86373
### **...**

0 commit comments

Comments
(0)

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