Skip to content

Commit f69c63a

Browse files
committed
feat: update leetcode solutions: No.0173. Binary Search Tree Iterator
1 parent cb4f8b9 commit f69c63a

File tree

7 files changed

+229
-31
lines changed

7 files changed

+229
-31
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
- [从前序与中序遍历序列构造二叉树](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README.md)
117117
- [从中序与后序遍历序列构造二叉树](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README.md)
118118
- [二叉搜索树的后序遍历序列](/lcof/面试题33.%20二叉搜索树的后序遍历序列/README.md)
119+
- [二叉搜索树迭代器](/solution/0100-0199/0173.Binary%20Search%20Tree%20Iterator/README.md)
119120
- [二叉树的最近公共祖先](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)
120121
- [二叉搜索树的最近公共祖先](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md)
121122
- [将二叉搜索树转换为单链表](/lcci/17.12.BiNode/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
112112
- [Path Sum II](/solution/0100-0199/0113.Path%20Sum%20II/README_EN.md)
113113
- [Construct Binary Tree from Preorder and Inorder Traversal](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README_EN.md)
114114
- [Construct Binary Tree from Inorder and Postorder Traversal](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README_EN.md)
115+
- [Binary Search Tree Iterator](/solution/0100-0199/0173.Binary%20Search%20Tree%20Iterator/README_EN.md)
115116
- [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)
116117
- [Lowest Common Ancestor of a Binary Search Tree](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README_EN.md)
117118
- [BiNode](/lcci/17.12.BiNode/README_EN.md)

index.html

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<script>
2727
window.$docsify = {
2828
name: 'leetcode',
29-
repo: 'doocs/leetcode',
3029
logo: '/images/doocs-leetcode.png',
3130
search: [
3231
'/', '/solution/', '/lcof/', '/lcci/', '/basic/'

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

+87-2
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,107 @@ iterator.hasNext(); // 返回 false</pre>
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
初始化数据时,递归中序遍历,将二叉搜索树每个结点的值保存在列表 `vals` 中。用 `cur/next` 指针记录外部即将遍历的位置,初始化为 0。
43+
44+
调用 `next()` 时,返回 `vals[cur]`,同时 `cur` 指针自增。调用 `hasNext()` 时,判断 `cur` 指针是否已经达到 `vals` 个数,若是,说明已经遍历结束,返回 false,否则返回 true。
45+
4246
<!-- tabs:start -->
4347

4448
### **Python3**
4549

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

4852
```python
49-
53+
# Definition for a binary tree node.
54+
# class TreeNode:
55+
# def __init__(self, val=0, left=None, right=None):
56+
# self.val = val
57+
# self.left = left
58+
# self.right = right
59+
class BSTIterator:
60+
61+
def __init__(self, root: TreeNode):
62+
def inorder(root):
63+
if root is None:
64+
return
65+
inorder(root.left)
66+
self.vals.append(root.val)
67+
inorder(root.right)
68+
69+
self.cur = 0
70+
self.vals = []
71+
inorder(root)
72+
73+
def next(self) -> int:
74+
res = self.vals[self.cur]
75+
self.cur += 1
76+
return res
77+
78+
def hasNext(self) -> bool:
79+
return self.cur < len(self.vals)
80+
81+
82+
# Your BSTIterator object will be instantiated and called as such:
83+
# obj = BSTIterator(root)
84+
# param_1 = obj.next()
85+
# param_2 = obj.hasNext()
5086
```
5187

5288
### **Java**
5389

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

5692
```java
57-
93+
/**
94+
* Definition for a binary tree node.
95+
* public class TreeNode {
96+
* int val;
97+
* TreeNode left;
98+
* TreeNode right;
99+
* TreeNode() {}
100+
* TreeNode(int val) { this.val = val; }
101+
* TreeNode(int val, TreeNode left, TreeNode right) {
102+
* this.val = val;
103+
* this.left = left;
104+
* this.right = right;
105+
* }
106+
* }
107+
*/
108+
class BSTIterator {
109+
110+
private List<Integer> vals;
111+
private int next;
112+
113+
public BSTIterator(TreeNode root) {
114+
next = 0;
115+
vals = new ArrayList<>();
116+
inorder(root);
117+
}
118+
119+
public int next() {
120+
return vals.get(next++);
121+
}
122+
123+
public boolean hasNext() {
124+
return next < vals.size();
125+
}
126+
127+
private void inorder(TreeNode root) {
128+
if (root == null) {
129+
return;
130+
}
131+
inorder(root.left);
132+
vals.add(root.val);
133+
inorder(root.right);
134+
}
135+
}
136+
137+
/**
138+
* Your BSTIterator object will be instantiated and called as such:
139+
* BSTIterator obj = new BSTIterator(root);
140+
* int param_1 = obj.next();
141+
* boolean param_2 = obj.hasNext();
142+
*/
58143
```
59144

60145
### **...**

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

+83-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,94 @@ iterator.hasNext(); // return false
5858
### **Python3**
5959

6060
```python
61-
61+
# Definition for a binary tree node.
62+
# class TreeNode:
63+
# def __init__(self, val=0, left=None, right=None):
64+
# self.val = val
65+
# self.left = left
66+
# self.right = right
67+
class BSTIterator:
68+
69+
def __init__(self, root: TreeNode):
70+
def inorder(root):
71+
if root is None:
72+
return
73+
inorder(root.left)
74+
self.vals.append(root.val)
75+
inorder(root.right)
76+
77+
self.cur = 0
78+
self.vals = []
79+
inorder(root)
80+
81+
def next(self) -> int:
82+
res = self.vals[self.cur]
83+
self.cur += 1
84+
return res
85+
86+
def hasNext(self) -> bool:
87+
return self.cur < len(self.vals)
88+
89+
90+
# Your BSTIterator object will be instantiated and called as such:
91+
# obj = BSTIterator(root)
92+
# param_1 = obj.next()
93+
# param_2 = obj.hasNext()
6294
```
6395

6496
### **Java**
6597

6698
```java
67-
99+
/**
100+
* Definition for a binary tree node.
101+
* public class TreeNode {
102+
* int val;
103+
* TreeNode left;
104+
* TreeNode right;
105+
* TreeNode() {}
106+
* TreeNode(int val) { this.val = val; }
107+
* TreeNode(int val, TreeNode left, TreeNode right) {
108+
* this.val = val;
109+
* this.left = left;
110+
* this.right = right;
111+
* }
112+
* }
113+
*/
114+
class BSTIterator {
115+
116+
private List<Integer> vals;
117+
private int next;
118+
119+
public BSTIterator(TreeNode root) {
120+
next = 0;
121+
vals = new ArrayList<>();
122+
inorder(root);
123+
}
124+
125+
public int next() {
126+
return vals.get(next++);
127+
}
128+
129+
public boolean hasNext() {
130+
return next < vals.size();
131+
}
132+
133+
private void inorder(TreeNode root) {
134+
if (root == null) {
135+
return;
136+
}
137+
inorder(root.left);
138+
vals.add(root.val);
139+
inorder(root.right);
140+
}
141+
}
142+
143+
/**
144+
* Your BSTIterator object will be instantiated and called as such:
145+
* BSTIterator obj = new BSTIterator(root);
146+
* int param_1 = obj.next();
147+
* boolean param_2 = obj.hasNext();
148+
*/
68149
```
69150

70151
### **...**

solution/0100-0199/0173.Binary Search Tree Iterator/Solution.java

+24-26
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,41 @@
44
* int val;
55
* TreeNode left;
66
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
1016
class BSTIterator {
1117

12-
Stack<TreeNode> vector = new Stack<>();
13-
TreeNode current;
18+
private List<Integer> vals;
19+
private int next;
1420

1521
public BSTIterator(TreeNode root) {
16-
current = root;
17-
// 一直放入左儿子(左)
18-
while (current != null) {
19-
vector.push(current);
20-
current = current.left;
21-
}
22+
next = 0;
23+
vals = new ArrayList<>();
24+
inorder(root);
2225
}
23-
24-
/** @return whether we have a next smallest number */
26+
27+
public int next() {
28+
return vals.get(next++);
29+
}
30+
2531
public boolean hasNext() {
26-
return !vector.isEmpty() || current != null;
32+
return next < vals.size();
2733
}
2834

29-
/** @return the next smallest number */
30-
public int next() {
31-
// 一直放入左儿子(左)
32-
while (current != null) {
33-
vector.push(current);
34-
current = current.left;
35-
}
36-
int ans = 0;
37-
// 访问当前元素(中),把右儿子入栈(右)
38-
if (!vector.isEmpty()) {
39-
current = vector.pop();
40-
ans = current.val;
41-
current = current.right;
35+
private void inorder(TreeNode root) {
36+
if (root == null) {
37+
return;
4238
}
43-
return ans;
39+
inorder(root.left);
40+
vals.add(root.val);
41+
inorder(root.right);
4442
}
4543
}
4644

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class BSTIterator:
8+
9+
def __init__(self, root: TreeNode):
10+
def inorder(root):
11+
if root is None:
12+
return
13+
inorder(root.left)
14+
self.vals.append(root.val)
15+
inorder(root.right)
16+
17+
self.cur = 0
18+
self.vals = []
19+
inorder(root)
20+
21+
def next(self) -> int:
22+
res = self.vals[self.cur]
23+
self.cur += 1
24+
return res
25+
26+
def hasNext(self) -> bool:
27+
return self.cur < len(self.vals)
28+
29+
30+
# Your BSTIterator object will be instantiated and called as such:
31+
# obj = BSTIterator(root)
32+
# param_1 = obj.next()
33+
# param_2 = obj.hasNext()

0 commit comments

Comments
 (0)