Skip to content

Commit 3a0b834

Browse files
committed
feat: add solutions to lc problem: No.0653
No.0653.Two Sum IV - Input is a BST
1 parent ff7ee7c commit 3a0b834

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

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

+79
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,40 @@ function find(root: TreeNode | null, k: number, nodes: Set<number>): boolean {
139139
}
140140
```
141141

142+
```ts
143+
/**
144+
* Definition for a binary tree node.
145+
* class TreeNode {
146+
* val: number
147+
* left: TreeNode | null
148+
* right: TreeNode | null
149+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
150+
* this.val = (val===undefined ? 0 : val)
151+
* this.left = (left===undefined ? null : left)
152+
* this.right = (right===undefined ? null : right)
153+
* }
154+
* }
155+
*/
156+
157+
function findTarget(root: TreeNode | null, k: number): boolean {
158+
if (root == null) {
159+
return false;
160+
}
161+
const set = new Set<number>();
162+
const dfs = (root: TreeNode | null) => {
163+
if (root == null) {
164+
return false;
165+
}
166+
if (set.has(root.val)) {
167+
return true;
168+
}
169+
set.add(k - root.val);
170+
return dfs(root.left) || dfs(root.right);
171+
};
172+
return dfs(root);
173+
}
174+
```
175+
142176
### **C++**
143177

144178
```cpp
@@ -199,6 +233,51 @@ func findTarget(root *TreeNode, k int) bool {
199233
}
200234
```
201235

236+
### **Rust**
237+
238+
```rust
239+
// Definition for a binary tree node.
240+
// #[derive(Debug, PartialEq, Eq)]
241+
// pub struct TreeNode {
242+
// pub val: i32,
243+
// pub left: Option<Rc<RefCell<TreeNode>>>,
244+
// pub right: Option<Rc<RefCell<TreeNode>>>,
245+
// }
246+
//
247+
// impl TreeNode {
248+
// #[inline]
249+
// pub fn new(val: i32) -> Self {
250+
// TreeNode {
251+
// val,
252+
// left: None,
253+
// right: None
254+
// }
255+
// }
256+
// }
257+
use std::rc::Rc;
258+
use std::cell::RefCell;
259+
use std::collections::{HashSet, VecDeque};
260+
impl Solution {
261+
pub fn find_target(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> bool {
262+
let mut set = HashSet::new();
263+
let mut q = VecDeque::new();
264+
q.push_back(root);
265+
while let Some(node) = q.pop_front() {
266+
if let Some(node) = node {
267+
let mut node = node.as_ref().borrow_mut();
268+
if set.contains(&node.val) {
269+
return true;
270+
}
271+
set.insert(k - node.val);
272+
q.push_back(node.left.take());
273+
q.push_back(node.right.take());
274+
}
275+
}
276+
false
277+
}
278+
}
279+
```
280+
202281
### **...**
203282

204283
```

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

+79
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,40 @@ function find(root: TreeNode | null, k: number, nodes: Set<number>): boolean {
127127
}
128128
```
129129

130+
```ts
131+
/**
132+
* Definition for a binary tree node.
133+
* class TreeNode {
134+
* val: number
135+
* left: TreeNode | null
136+
* right: TreeNode | null
137+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
138+
* this.val = (val===undefined ? 0 : val)
139+
* this.left = (left===undefined ? null : left)
140+
* this.right = (right===undefined ? null : right)
141+
* }
142+
* }
143+
*/
144+
145+
function findTarget(root: TreeNode | null, k: number): boolean {
146+
if (root == null) {
147+
return false;
148+
}
149+
const set = new Set<number>();
150+
const dfs = (root: TreeNode | null) => {
151+
if (root == null) {
152+
return false;
153+
}
154+
if (set.has(root.val)) {
155+
return true;
156+
}
157+
set.add(k - root.val);
158+
return dfs(root.left) || dfs(root.right);
159+
};
160+
return dfs(root);
161+
}
162+
```
163+
130164
### **C++**
131165

132166
```cpp
@@ -187,6 +221,51 @@ func findTarget(root *TreeNode, k int) bool {
187221
}
188222
```
189223

224+
### **Rust**
225+
226+
```rust
227+
// Definition for a binary tree node.
228+
// #[derive(Debug, PartialEq, Eq)]
229+
// pub struct TreeNode {
230+
// pub val: i32,
231+
// pub left: Option<Rc<RefCell<TreeNode>>>,
232+
// pub right: Option<Rc<RefCell<TreeNode>>>,
233+
// }
234+
//
235+
// impl TreeNode {
236+
// #[inline]
237+
// pub fn new(val: i32) -> Self {
238+
// TreeNode {
239+
// val,
240+
// left: None,
241+
// right: None
242+
// }
243+
// }
244+
// }
245+
use std::rc::Rc;
246+
use std::cell::RefCell;
247+
use std::collections::{HashSet, VecDeque};
248+
impl Solution {
249+
pub fn find_target(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> bool {
250+
let mut set = HashSet::new();
251+
let mut q = VecDeque::new();
252+
q.push_back(root);
253+
while let Some(node) = q.pop_front() {
254+
if let Some(node) = node {
255+
let mut node = node.as_ref().borrow_mut();
256+
if set.contains(&node.val) {
257+
return true;
258+
}
259+
set.insert(k - node.val);
260+
q.push_back(node.left.take());
261+
q.push_back(node.right.take());
262+
}
263+
}
264+
false
265+
}
266+
}
267+
```
268+
190269
### **...**
191270

192271
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::cell::RefCell;
20+
use std::collections::{HashSet, VecDeque};
21+
use std::rc::Rc;
22+
impl Solution {
23+
pub fn find_target(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> bool {
24+
let mut set = HashSet::new();
25+
let mut q = VecDeque::new();
26+
q.push_back(root);
27+
while let Some(node) = q.pop_front() {
28+
if let Some(node) = node {
29+
let mut node = node.as_ref().borrow_mut();
30+
if set.contains(&node.val) {
31+
return true;
32+
}
33+
set.insert(k - node.val);
34+
q.push_back(node.left.take());
35+
q.push_back(node.right.take());
36+
}
37+
}
38+
false
39+
}
40+
}

0 commit comments

Comments
 (0)