-
Notifications
You must be signed in to change notification settings - Fork 3
/
21-two-sum-iv-input-is-a-bst.rs
42 lines (41 loc) · 1.18 KB
/
21-two-sum-iv-input-is-a-bst.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashSet;
impl Solution {
pub fn find_target(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> bool {
let mut set = HashSet::new();
return Self::dfs(&root, k, &mut set);
}
pub fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, k: i32, set: &mut HashSet<i32>) -> bool {
match node {
None => false,
Some(x) => {
if set.contains(&(k - x.borrow().val)) {
true
} else {
set.insert(x.borrow().val);
Self::dfs(&x.borrow().left, k, set) || Self::dfs(&x.borrow().right, k, set)
}
}
}
}
}