-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 623: Add One Row to Tree
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use super::data_structures::TreeNode; | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
pub mod recursive; | ||
|
||
pub trait Solution { | ||
fn add_one_row(root: Option<Rc<RefCell<TreeNode>>>, val: i32, depth: i32) -> Option<Rc<RefCell<TreeNode>>>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::super::test_utilities; | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
(&[Some(4), Some(2), Some(6), Some(3), Some(1), Some(5)] as &[_], 1, 2), | ||
&[ | ||
Some(4), | ||
Some(1), | ||
Some(1), | ||
Some(2), | ||
None, | ||
None, | ||
Some(6), | ||
Some(3), | ||
Some(1), | ||
Some(5), | ||
] as &[_], | ||
), | ||
( | ||
(&[Some(4), Some(2), None, Some(3), Some(1)], 1, 3), | ||
&[Some(4), Some(2), None, Some(1), Some(1), Some(3), None, None, Some(1)], | ||
), | ||
]; | ||
|
||
for ((root, val, depth), expected) in test_cases.iter().copied() { | ||
assert_eq!( | ||
S::add_one_row(test_utilities::make_tree(root.iter().copied()), val, depth), | ||
test_utilities::make_tree(expected.iter().copied()) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use super::super::data_structures::TreeNode; | ||
|
||
pub struct Solution; | ||
|
||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
impl Solution { | ||
fn new_node_with_left_child(val: i32, node: Option<Rc<RefCell<TreeNode>>>) -> TreeNode { | ||
TreeNode { | ||
val, | ||
left: node, | ||
right: None, | ||
} | ||
} | ||
|
||
fn new_node_with_right_child(val: i32, node: Option<Rc<RefCell<TreeNode>>>) -> TreeNode { | ||
TreeNode { | ||
val, | ||
left: None, | ||
right: node, | ||
} | ||
} | ||
|
||
fn helper( | ||
node: &mut Option<Rc<RefCell<TreeNode>>>, | ||
val: i32, | ||
depth: i32, | ||
make_child: impl FnOnce(i32, Option<Rc<RefCell<TreeNode>>>) -> TreeNode, | ||
) { | ||
if depth == 1 { | ||
*node = Some(Rc::new(RefCell::new(make_child(val, node.take())))); | ||
} else if let Some(mut node) = node.as_deref().map(RefCell::borrow_mut) { | ||
Self::helper(&mut node.left, val, depth - 1, Self::new_node_with_left_child); | ||
Self::helper(&mut node.right, val, depth - 1, Self::new_node_with_right_child); | ||
} | ||
} | ||
|
||
pub fn add_one_row(mut root: Option<Rc<RefCell<TreeNode>>>, val: i32, depth: i32) -> Option<Rc<RefCell<TreeNode>>> { | ||
Self::helper(&mut root, val, depth, Self::new_node_with_left_child); | ||
|
||
root | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn add_one_row(root: Option<Rc<RefCell<TreeNode>>>, val: i32, depth: i32) -> Option<Rc<RefCell<TreeNode>>> { | ||
Self::add_one_row(root, val, depth) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |