Skip to content

Commit 234d667

Browse files
committed
feat: add solutions to lc problem: No.0272
No.0272.Closest Binary Search Tree Value II
1 parent 5a5333d commit 234d667

File tree

12 files changed

+463
-15
lines changed

12 files changed

+463
-15
lines changed

.github/workflows/compress.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Compress
1+
name: compress-images
22

33
on:
44
schedule:

.github/workflows/contributors.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Contributors
1+
name: update-contributors
22

33
on:
44
schedule:

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Create Release
1+
name: create-release
22

33
on:
44
push:

.github/workflows/starcharts.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Starcharts
1+
name: update-starcharts
22

33
on:
44
schedule:

.github/workflows/sync.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Sync
1+
name: sync
22

33
on:
44
push:

solution/0200-0299/0272.Closest Binary Search Tree Value II/README.md

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,181 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
中序遍历,当结果元素个数小于 k 时,直接添加。否则,拿第一个元素与当前节点 root 各自与 target 的差值的绝对值进行比较。
39+
40+
- 若当前节点 root 与目标值的差值的绝对值大于等于第一个节点与目标值差值的绝对值,移除第一个元素,然后添加当前节点 root.val。
41+
- 否则,无需再遍历后面的节点。
42+
43+
时间复杂度 O(n),空间复杂度 O(k)。
44+
3845
<!-- tabs:start -->
3946

4047
### **Python3**
4148

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

4451
```python
45-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, val=0, left=None, right=None):
55+
# self.val = val
56+
# self.left = left
57+
# self.right = right
58+
class Solution:
59+
def closestKValues(self, root: TreeNode, target: float, k: int) -> List[int]:
60+
def dfs(root):
61+
if root is None:
62+
return
63+
dfs(root.left)
64+
if len(q) < k:
65+
q.append(root.val)
66+
else:
67+
if abs(root.val - target) >= abs(q[0] - target):
68+
return
69+
q.popleft()
70+
q.append(root.val)
71+
dfs(root.right)
72+
73+
q = deque()
74+
dfs(root)
75+
return list(q)
4676
```
4777

4878
### **Java**
4979

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

5282
```java
83+
/**
84+
* Definition for a binary tree node.
85+
* public class TreeNode {
86+
* int val;
87+
* TreeNode left;
88+
* TreeNode right;
89+
* TreeNode() {}
90+
* TreeNode(int val) { this.val = val; }
91+
* TreeNode(int val, TreeNode left, TreeNode right) {
92+
* this.val = val;
93+
* this.left = left;
94+
* this.right = right;
95+
* }
96+
* }
97+
*/
98+
class Solution {
99+
100+
private List<Integer> ans;
101+
private double target;
102+
private int k;
103+
104+
public List<Integer> closestKValues(TreeNode root, double target, int k) {
105+
ans = new LinkedList<>();
106+
this.target = target;
107+
this.k = k;
108+
dfs(root);
109+
return ans;
110+
}
111+
112+
private void dfs(TreeNode root) {
113+
if (root == null) {
114+
return;
115+
}
116+
dfs(root.left);
117+
if (ans.size() < k) {
118+
ans.add(root.val);
119+
} else {
120+
if (Math.abs(root.val - target) >= Math.abs(ans.get(0) - target)) {
121+
return;
122+
}
123+
ans.remove(0);
124+
ans.add(root.val);
125+
}
126+
dfs(root.right);
127+
}
128+
}
129+
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
/**
136+
* Definition for a binary tree node.
137+
* struct TreeNode {
138+
* int val;
139+
* TreeNode *left;
140+
* TreeNode *right;
141+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
142+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
143+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
144+
* };
145+
*/
146+
class Solution {
147+
public:
148+
queue<int> q;
149+
double target;
150+
int k;
151+
152+
vector<int> closestKValues(TreeNode* root, double target, int k) {
153+
this->target = target;
154+
this->k = k;
155+
dfs(root);
156+
vector<int> ans;
157+
while (!q.empty())
158+
{
159+
ans.push_back(q.front());
160+
q.pop();
161+
}
162+
return ans;
163+
}
164+
165+
void dfs(TreeNode* root) {
166+
if (!root) return;
167+
dfs(root->left);
168+
if (q.size() < k) q.push(root->val);
169+
else
170+
{
171+
if (abs(root->val - target) >= abs(q.front() - target)) return;
172+
q.pop();
173+
q.push(root->val);
174+
}
175+
dfs(root->right);
176+
}
177+
};
178+
```
53179
180+
### **Go**
181+
182+
```go
183+
/**
184+
* Definition for a binary tree node.
185+
* type TreeNode struct {
186+
* Val int
187+
* Left *TreeNode
188+
* Right *TreeNode
189+
* }
190+
*/
191+
func closestKValues(root *TreeNode, target float64, k int) []int {
192+
var ans []int
193+
var dfs func(root *TreeNode)
194+
dfs = func(root *TreeNode) {
195+
if root == nil {
196+
return
197+
}
198+
dfs(root.Left)
199+
if len(ans) < k {
200+
ans = append(ans, root.Val)
201+
} else {
202+
if math.Abs(float64(root.Val)-target) >= math.Abs(float64(ans[0])-target) {
203+
return
204+
}
205+
ans = ans[1:]
206+
ans = append(ans, root.Val)
207+
}
208+
dfs(root.Right)
209+
}
210+
dfs(root)
211+
return ans
212+
}
54213
```
55214

56215
### **...**

solution/0200-0299/0272.Closest Binary Search Tree Value II/README_EN.md

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,165 @@
4343
### **Python3**
4444

4545
```python
46-
46+
# Definition for a binary tree node.
47+
# class TreeNode:
48+
# def __init__(self, val=0, left=None, right=None):
49+
# self.val = val
50+
# self.left = left
51+
# self.right = right
52+
class Solution:
53+
def closestKValues(self, root: TreeNode, target: float, k: int) -> List[int]:
54+
def dfs(root):
55+
if root is None:
56+
return
57+
dfs(root.left)
58+
if len(q) < k:
59+
q.append(root.val)
60+
else:
61+
if abs(root.val - target) >= abs(q[0] - target):
62+
return
63+
q.popleft()
64+
q.append(root.val)
65+
dfs(root.right)
66+
67+
q = deque()
68+
dfs(root)
69+
return list(q)
4770
```
4871

4972
### **Java**
5073

5174
```java
75+
/**
76+
* Definition for a binary tree node.
77+
* public class TreeNode {
78+
* int val;
79+
* TreeNode left;
80+
* TreeNode right;
81+
* TreeNode() {}
82+
* TreeNode(int val) { this.val = val; }
83+
* TreeNode(int val, TreeNode left, TreeNode right) {
84+
* this.val = val;
85+
* this.left = left;
86+
* this.right = right;
87+
* }
88+
* }
89+
*/
90+
class Solution {
91+
92+
private List<Integer> ans;
93+
private double target;
94+
private int k;
95+
96+
public List<Integer> closestKValues(TreeNode root, double target, int k) {
97+
ans = new LinkedList<>();
98+
this.target = target;
99+
this.k = k;
100+
dfs(root);
101+
return ans;
102+
}
103+
104+
private void dfs(TreeNode root) {
105+
if (root == null) {
106+
return;
107+
}
108+
dfs(root.left);
109+
if (ans.size() < k) {
110+
ans.add(root.val);
111+
} else {
112+
if (Math.abs(root.val - target) >= Math.abs(ans.get(0) - target)) {
113+
return;
114+
}
115+
ans.remove(0);
116+
ans.add(root.val);
117+
}
118+
dfs(root.right);
119+
}
120+
}
121+
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
/**
128+
* Definition for a binary tree node.
129+
* struct TreeNode {
130+
* int val;
131+
* TreeNode *left;
132+
* TreeNode *right;
133+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
134+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
135+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
136+
* };
137+
*/
138+
class Solution {
139+
public:
140+
queue<int> q;
141+
double target;
142+
int k;
143+
144+
vector<int> closestKValues(TreeNode* root, double target, int k) {
145+
this->target = target;
146+
this->k = k;
147+
dfs(root);
148+
vector<int> ans;
149+
while (!q.empty())
150+
{
151+
ans.push_back(q.front());
152+
q.pop();
153+
}
154+
return ans;
155+
}
156+
157+
void dfs(TreeNode* root) {
158+
if (!root) return;
159+
dfs(root->left);
160+
if (q.size() < k) q.push(root->val);
161+
else
162+
{
163+
if (abs(root->val - target) >= abs(q.front() - target)) return;
164+
q.pop();
165+
q.push(root->val);
166+
}
167+
dfs(root->right);
168+
}
169+
};
170+
```
52171
172+
### **Go**
173+
174+
```go
175+
/**
176+
* Definition for a binary tree node.
177+
* type TreeNode struct {
178+
* Val int
179+
* Left *TreeNode
180+
* Right *TreeNode
181+
* }
182+
*/
183+
func closestKValues(root *TreeNode, target float64, k int) []int {
184+
var ans []int
185+
var dfs func(root *TreeNode)
186+
dfs = func(root *TreeNode) {
187+
if root == nil {
188+
return
189+
}
190+
dfs(root.Left)
191+
if len(ans) < k {
192+
ans = append(ans, root.Val)
193+
} else {
194+
if math.Abs(float64(root.Val)-target) >= math.Abs(float64(ans[0])-target) {
195+
return
196+
}
197+
ans = ans[1:]
198+
ans = append(ans, root.Val)
199+
}
200+
dfs(root.Right)
201+
}
202+
dfs(root)
203+
return ans
204+
}
53205
```
54206

55207
### **...**

0 commit comments

Comments
 (0)