Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 30a04ff

Browse files
author
guangsheng.li01
committed
.
1 parent adec5c2 commit 30a04ff

File tree

5 files changed

+201
-10
lines changed

5 files changed

+201
-10
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* [0701] insert-into-a-binary-search-tree
3+
*/
4+
5+
use super::utils::tree::*;
6+
struct Solution;
7+
8+
use std::cell::RefCell;
9+
use std::rc::Rc;
10+
impl Solution {
11+
pub fn insert_into_bst(
12+
root: Option<Rc<RefCell<TreeNode>>>,
13+
val: i32,
14+
) -> Option<Rc<RefCell<TreeNode>>> {
15+
if let Some(root) = root.clone() {
16+
let mut root = root.borrow_mut();
17+
18+
if val < root.val {
19+
if root.left.is_none() {
20+
root.left = Some(Rc::new(RefCell::new(TreeNode {
21+
left: Option::None,
22+
right: Option::None,
23+
val: val,
24+
})))
25+
} else {
26+
Self::insert_into_bst(root.left.clone(), val);
27+
}
28+
} else {
29+
if root.right.is_none() {
30+
root.right = Some(Rc::new(RefCell::new(TreeNode {
31+
left: Option::None,
32+
right: Option::None,
33+
val: val,
34+
})))
35+
} else {
36+
Self::insert_into_bst(root.right.clone(), val);
37+
}
38+
}
39+
}
40+
root
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
48+
#[test]
49+
fn test_case0() {
50+
assert_eq!(
51+
Solution::insert_into_bst(tree![4, 2, 7, 1, 3], 5),
52+
tree![4, 2, 7, 1, 3, 5]
53+
);
54+
}
55+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* [0894] all-possible-full-binary-trees
3+
*/
4+
5+
use super::utils::tree::*;
6+
struct Solution;
7+
8+
use std::cell::RefCell;
9+
use std::collections::HashMap;
10+
use std::rc::Rc;
11+
impl Solution {
12+
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
13+
let mut m: HashMap<i32, Vec<Option<Rc<RefCell<TreeNode>>>>> = HashMap::new();
14+
Self::get(n, &mut m)
15+
}
16+
17+
fn get(
18+
n: i32,
19+
m: &mut HashMap<i32, Vec<Option<Rc<RefCell<TreeNode>>>>>,
20+
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
21+
if !m.contains_key(&n) {
22+
let mut ans = Vec::new();
23+
if n == 1 {
24+
ans.push(Some(Rc::new(RefCell::new(TreeNode {
25+
left: Option::None,
26+
right: Option::None,
27+
val: 0,
28+
}))));
29+
} else if n % 2 == 1 {
30+
for x in 0..n {
31+
let y = n - 1 - x;
32+
for left in Self::all_possible_fbt(x) {
33+
for right in Self::all_possible_fbt(y) {
34+
let n = Some(Rc::new(RefCell::new(TreeNode {
35+
left: left.clone(),
36+
right: right.clone(),
37+
val: 0,
38+
})));
39+
ans.push(n);
40+
}
41+
}
42+
}
43+
}
44+
m.insert(n, ans);
45+
}
46+
m.get(&n).unwrap().to_vec()
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
fn test_case0() {
56+
assert_eq!(Solution::all_possible_fbt(1), vec![tree![0]]);
57+
assert_eq!(
58+
Solution::all_possible_fbt(7),
59+
vec![
60+
tree![0, 0, 0, null, null, 0, 0, null, null, 0, 0],
61+
tree![0, 0, 0, null, null, 0, 0, 0, 0],
62+
tree![0, 0, 0, 0, 0, 0, 0],
63+
tree![0, 0, 0, 0, 0, null, null, null, null, 0, 0],
64+
tree![0, 0, 0, 0, 0, null, null, 0, 0]
65+
]
66+
);
67+
}
68+
}

‎src/a0938_range_sum_of_bst.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* [0938] range-sum-of-bst
3+
*/
4+
5+
use super::utils::tree::*;
6+
pub struct Solution {}
7+
8+
// solution impl starts here
9+
10+
// Definition for a binary tree node.
11+
// #[derive(Debug, PartialEq, Eq)]
12+
// pub struct TreeNode {
13+
// pub val: i32,
14+
// pub left: Option<Rc<RefCell<TreeNode>>>,
15+
// pub right: Option<Rc<RefCell<TreeNode>>>,
16+
// }
17+
//
18+
// impl TreeNode {
19+
// #[inline]
20+
// pub fn new(val: i32) -> Self {
21+
// TreeNode {
22+
// val,
23+
// left: None,
24+
// right: None
25+
// }
26+
// }
27+
// }
28+
use std::cell::RefCell;
29+
use std::rc::Rc;
30+
impl Solution {
31+
pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32) -> i32 {
32+
let mut sum = 0;
33+
Self::dfs(root, l, r, &mut sum);
34+
sum
35+
}
36+
37+
fn dfs(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32, sum: &mut i32) {
38+
if let Some(root) = root {
39+
let root = root.borrow();
40+
if root.val >= l && root.val <= r {
41+
*sum += root.val;
42+
}
43+
if root.val > l {
44+
Self::dfs(root.left.clone(), l, r, sum);
45+
}
46+
if root.val < r {
47+
Self::dfs(root.right.clone(), l, r, sum);
48+
}
49+
}
50+
}
51+
}
52+
// solution impl ends here
53+
54+
// solution tests starts here
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
60+
#[test]
61+
fn test_case0() {
62+
assert_eq!(
63+
Solution::range_sum_bst(tree![10, 5, 15, 3, 7, null, 18], 7, 15),
64+
32
65+
);
66+
assert_eq!(
67+
Solution::range_sum_bst(tree![10, 5, 15, 3, 7, 13, 18, 1, null, 6], 6, 10),
68+
23
69+
);
70+
}
71+
}
72+
73+
// solution tests ends here

‎src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ mod a0617_merge_two_binary_trees;
3333
mod a0643_maximum_average_subarray_i;
3434
mod a0646_maximum_length_of_pair_chain;
3535
mod a0654_maximum_binary_tree;
36+
mod a0701_insert_into_a_binary_search_tree;
3637
mod a0867_transpose_matrix;
3738
mod a0883_projection_area_of_3d_shapes;
39+
mod a0894_all_possible_full_binary_trees;
3840
mod a0929_unique_email_addresses;
3941
mod a0931_minimum_falling_path_sum;
4042
mod a0937_reorder_data_in_log_files;
43+
mod a0938_range_sum_of_bst;
4144
mod a0949_largest_time_for_given_digits;
4245
mod a0973_k_closest_points_to_origin;
4346
mod a0980_unique_paths_iii;

‎src/main.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,23 @@ const TEMPLATE: &str = "/*
1212
* [{{ problem_id }}] {{ problem_title }}
1313
*/
1414
15-
pub struct Solution {}
16-
17-
// solution impl starts here
15+
struct Solution;
1816
1917
impl Solution {
2018
fn add(a: i32, b: i32) -> i32 {
2119
a + b
2220
}
2321
}
2422
25-
// solution impl ends here
26-
27-
// solution tests starts here
28-
2923
#[cfg(test)]
3024
mod tests {
3125
use super::*;
3226
3327
#[test]
3428
fn test_case0() {
35-
assert_eq!(5, Solution::add(2, 3));
29+
assert_eq!(Solution::add(2, 3), 5);
3630
}
3731
}
38-
39-
// solution tests ends here
4032
";
4133

4234
fn main() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /