Skip to content

Commit abdc7f5

Browse files
committed
feat: add solutions to lc problems: No.0653,1663
* No.0653.Two Sum IV - Input is a BST * No.1663.Smallest String With A Given Numeric Value
1 parent b627e77 commit abdc7f5

File tree

13 files changed

+532
-259
lines changed

13 files changed

+532
-259
lines changed

solution/0600-0699/0653.Two Sum IV - Input is a BST/README.md

+233-87
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@
3939

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

42-
**方法一:哈希表**
42+
**方法一:哈希表 + DFS**
4343

44-
用哈希表记录访问过的节点
44+
DFS 遍历二叉搜索树,对于每个节点,判断 `k - node.val` 是否在哈希表中,如果在,则返回 `true`,否则将 `node.val` 加入哈希表中
4545

46-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。
46+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉搜索树的节点个数。
47+
48+
**方法二:哈希表 + BFS**
49+
50+
与方法一类似,只是使用 BFS 遍历二叉搜索树。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉搜索树的节点个数。
4753

4854
<!-- tabs:start -->
4955

@@ -59,17 +65,41 @@
5965
# self.left = left
6066
# self.right = right
6167
class Solution:
62-
def findTarget(self, root: TreeNode, k: int) -> bool:
63-
def find(root):
64-
if not root:
68+
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
69+
def dfs(root):
70+
if root is None:
6571
return False
66-
if k - root.val in nodes:
72+
if k - root.val in vis:
6773
return True
68-
nodes.add(root.val)
69-
return find(root.left) or find(root.right)
74+
vis.add(root.val)
75+
return dfs(root.left) or dfs(root.right)
7076

71-
nodes = set()
72-
return find(root)
77+
vis = set()
78+
return dfs(root)
79+
```
80+
81+
```python
82+
# Definition for a binary tree node.
83+
# class TreeNode:
84+
# def __init__(self, val=0, left=None, right=None):
85+
# self.val = val
86+
# self.left = left
87+
# self.right = right
88+
class Solution:
89+
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
90+
q = deque([root])
91+
vis = set()
92+
while q:
93+
for _ in range(len(q)):
94+
node = q.popleft()
95+
if k - node.val in vis:
96+
return True
97+
vis.add(node.val)
98+
if node.left:
99+
q.append(node.left)
100+
if node.right:
101+
q.append(node.right)
102+
return False
73103
```
74104

75105
### **Java**
@@ -93,92 +123,102 @@ class Solution:
93123
* }
94124
*/
95125
class Solution {
96-
private Set<Integer> nodes;
126+
private Set<Integer> vis = new HashSet<>();
127+
private int k;
97128

98129
public boolean findTarget(TreeNode root, int k) {
99-
nodes = new HashSet<>();
100-
return find(root, k);
130+
this.k = k;
131+
return dfs(root);
101132
}
102133

103-
private boolean find(TreeNode root, int k) {
134+
private boolean dfs(TreeNode root) {
104135
if (root == null) {
105136
return false;
106137
}
107-
if (nodes.contains(k - root.val)) {
138+
if (vis.contains(k - root.val)) {
108139
return true;
109140
}
110-
nodes.add(root.val);
111-
return find(root.left, k) || find(root.right, k);
141+
vis.add(root.val);
142+
return dfs(root.left) || dfs(root.right);
112143
}
113144
}
114145
```
115146

116-
### **TypeScript**
117-
118-
```ts
147+
```java
119148
/**
120149
* Definition for a binary tree node.
121-
* class TreeNode {
122-
* val: number
123-
* left: TreeNode | null
124-
* right: TreeNode | null
125-
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
126-
* this.val = (val===undefined ? 0 : val)
127-
* this.left = (left===undefined ? null : left)
128-
* this.right = (right===undefined ? null : right)
150+
* public class TreeNode {
151+
* int val;
152+
* TreeNode left;
153+
* TreeNode right;
154+
* TreeNode() {}
155+
* TreeNode(int val) { this.val = val; }
156+
* TreeNode(int val, TreeNode left, TreeNode right) {
157+
* this.val = val;
158+
* this.left = left;
159+
* this.right = right;
129160
* }
130161
* }
131162
*/
132-
133-
function findTarget(root: TreeNode | null, k: number): boolean {
134-
let nodes: Set<number> = new Set();
135-
return find(root, k, nodes);
136-
}
137-
138-
function find(root: TreeNode | null, k: number, nodes: Set<number>): boolean {
139-
if (!root) return false;
140-
if (nodes.has(k - root.val)) return true;
141-
nodes.add(root.val);
142-
return find(root.left, k, nodes) || find(root.right, k, nodes);
163+
class Solution {
164+
public boolean findTarget(TreeNode root, int k) {
165+
Deque<TreeNode> q = new ArrayDeque<>();
166+
q.offer(root);
167+
Set<Integer> vis = new HashSet<>();
168+
while (!q.isEmpty()) {
169+
for (int n = q.size(); n > 0; --n) {
170+
TreeNode node = q.poll();
171+
if (vis.contains(k - node.val)) {
172+
return true;
173+
}
174+
vis.add(node.val);
175+
if (node.left != null) {
176+
q.offer(node.left);
177+
}
178+
if (node.right != null) {
179+
q.offer(node.right);
180+
}
181+
}
182+
}
183+
return false;
184+
}
143185
}
144186
```
145187

146-
```ts
188+
### **C++**
189+
190+
```cpp
147191
/**
148192
* Definition for a binary tree node.
149-
* class TreeNode {
150-
* val: number
151-
* left: TreeNode | null
152-
* right: TreeNode | null
153-
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
154-
* this.val = (val===undefined ? 0 : val)
155-
* this.left = (left===undefined ? null : left)
156-
* this.right = (right===undefined ? null : right)
157-
* }
158-
* }
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+
* };
159201
*/
202+
class Solution {
203+
public:
204+
bool findTarget(TreeNode* root, int k) {
205+
unordered_set<int> vis;
160206

161-
function findTarget(root: TreeNode | null, k: number): boolean {
162-
if (root == null) {
163-
return false;
207+
function<bool(TreeNode*)> dfs = [&](TreeNode* root) {
208+
if (!root) {
209+
return false;
210+
}
211+
if (vis.count(k - root->val)) {
212+
return true;
213+
}
214+
vis.insert(root->val);
215+
return dfs(root->left) || dfs(root->right);
216+
};
217+
return dfs(root);
164218
}
165-
const set = new Set<number>();
166-
const dfs = (root: TreeNode | null) => {
167-
if (root == null) {
168-
return false;
169-
}
170-
if (set.has(root.val)) {
171-
return true;
172-
}
173-
set.add(k - root.val);
174-
return dfs(root.left) || dfs(root.right);
175-
};
176-
return dfs(root);
177-
}
219+
};
178220
```
179221

180-
### **C++**
181-
182222
```cpp
183223
/**
184224
* Definition for a binary tree node.
@@ -193,17 +233,26 @@ function findTarget(root: TreeNode | null, k: number): boolean {
193233
*/
194234
class Solution {
195235
public:
196-
unordered_set<int> nodes;
197-
198236
bool findTarget(TreeNode* root, int k) {
199-
return find(root, k);
200-
}
201-
202-
bool find(TreeNode* root, int k) {
203-
if (!root) return false;
204-
if (nodes.count(k - root->val)) return true;
205-
nodes.insert(root->val);
206-
return find(root->left, k) || find(root->right, k);
237+
queue<TreeNode*> q{{root}};
238+
unordered_set<int> vis;
239+
while (!q.empty()) {
240+
for (int n = q.size(); n; --n) {
241+
TreeNode* node = q.front();
242+
q.pop();
243+
if (vis.count(k - node->val)) {
244+
return true;
245+
}
246+
vis.insert(node->val);
247+
if (node->left) {
248+
q.push(node->left);
249+
}
250+
if (node->right) {
251+
q.push(node->right);
252+
}
253+
}
254+
}
255+
return false;
207256
}
208257
};
209258
```
@@ -220,20 +269,117 @@ public:
220269
* }
221270
*/
222271
func findTarget(root *TreeNode, k int) bool {
223-
nodes := make(map[int]bool)
224-
225-
var find func(root *TreeNode, k int) bool
226-
find = func(root *TreeNode, k int) bool {
272+
vis := map[int]bool{}
273+
var dfs func(*TreeNode) bool
274+
dfs = func(root *TreeNode) bool {
227275
if root == nil {
228276
return false
229277
}
230-
if nodes[k-root.Val] {
278+
if vis[k-root.Val] {
231279
return true
232280
}
233-
nodes[root.Val] = true
234-
return find(root.Left, k) || find(root.Right, k)
281+
vis[root.Val] = true
282+
return dfs(root.Left) || dfs(root.Right)
283+
}
284+
return dfs(root)
285+
}
286+
```
287+
288+
```go
289+
/**
290+
* Definition for a binary tree node.
291+
* type TreeNode struct {
292+
* Val int
293+
* Left *TreeNode
294+
* Right *TreeNode
295+
* }
296+
*/
297+
func findTarget(root *TreeNode, k int) bool {
298+
q := []*TreeNode{root}
299+
vis := map[int]bool{}
300+
for len(q) > 0 {
301+
for n := len(q); n > 0; n-- {
302+
node := q[0]
303+
q = q[1:]
304+
if vis[k-node.Val] {
305+
return true
306+
}
307+
vis[node.Val] = true
308+
if node.Left != nil {
309+
q = append(q, node.Left)
310+
}
311+
if node.Right != nil {
312+
q = append(q, node.Right)
313+
}
314+
}
235315
}
236-
return find(root, k)
316+
return false
317+
}
318+
```
319+
320+
### **TypeScript**
321+
322+
```ts
323+
/**
324+
* Definition for a binary tree node.
325+
* class TreeNode {
326+
* val: number
327+
* left: TreeNode | null
328+
* right: TreeNode | null
329+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
330+
* this.val = (val===undefined ? 0 : val)
331+
* this.left = (left===undefined ? null : left)
332+
* this.right = (right===undefined ? null : right)
333+
* }
334+
* }
335+
*/
336+
337+
function findTarget(root: TreeNode | null, k: number): boolean {
338+
const dfs = (root: TreeNode | null) => {
339+
if (!root) {
340+
return false;
341+
}
342+
if (vis.has(k - root.val)) {
343+
return true;
344+
}
345+
vis.add(root.val);
346+
return dfs(root.left) || dfs(root.right);
347+
};
348+
const vis = new Set<number>();
349+
return dfs(root);
350+
}
351+
```
352+
353+
```ts
354+
/**
355+
* Definition for a binary tree node.
356+
* class TreeNode {
357+
* val: number
358+
* left: TreeNode | null
359+
* right: TreeNode | null
360+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
361+
* this.val = (val===undefined ? 0 : val)
362+
* this.left = (left===undefined ? null : left)
363+
* this.right = (right===undefined ? null : right)
364+
* }
365+
* }
366+
*/
367+
368+
function findTarget(root: TreeNode | null, k: number): boolean {
369+
const q = [root];
370+
const vis = new Set<number>();
371+
while (q.length) {
372+
for (let n = q.length; n; --n) {
373+
const { val, left, right } = q.shift();
374+
if (vis.has(k - val)) {
375+
return true;
376+
}
377+
vis.add(val);
378+
left && q.push(left);
379+
right && q.push(right);
380+
}
381+
}
382+
return false;
237383
}
238384
```
239385

0 commit comments

Comments
 (0)