-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 0235: Lowest Common Ancestor of a Binary Search Tree
- Loading branch information
Showing
3 changed files
with
144 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
49 changes: 49 additions & 0 deletions
49
src/problem_0235_lowest_common_ancestor_of_a_binary_search_tree/iterative.rs
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,49 @@ | ||
use crate::data_structures::TreeNode; | ||
|
||
pub struct Solution; | ||
|
||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
impl Solution { | ||
#[allow(clippy::unnecessary_wraps)] | ||
pub fn lowest_common_ancestor( | ||
root: Option<Rc<RefCell<TreeNode>>>, | ||
p: Option<Rc<RefCell<TreeNode>>>, | ||
q: Option<Rc<RefCell<TreeNode>>>, | ||
) -> Option<Rc<RefCell<TreeNode>>> { | ||
let p = p.unwrap().borrow().val; | ||
let q = q.unwrap().borrow().val; | ||
let left = p.min(q); | ||
let right = p.max(q); | ||
let mut root = root.unwrap(); | ||
loop { | ||
root = if root.borrow().val > right { | ||
root.borrow_mut().left.take().unwrap() | ||
} else if root.borrow().val < left { | ||
root.borrow_mut().right.take().unwrap() | ||
} else { | ||
break; | ||
} | ||
} | ||
Some(root) | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn lowest_common_ancestor( | ||
root: Option<Rc<RefCell<TreeNode>>>, | ||
p: Option<Rc<RefCell<TreeNode>>>, | ||
q: Option<Rc<RefCell<TreeNode>>>, | ||
) -> Option<Rc<RefCell<TreeNode>>> { | ||
Self::lowest_common_ancestor(root, p, q) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/problem_0235_lowest_common_ancestor_of_a_binary_search_tree/mod.rs
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,94 @@ | ||
pub mod iterative; | ||
|
||
use crate::data_structures::TreeNode; | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
pub trait Solution { | ||
fn lowest_common_ancestor( | ||
root: Option<Rc<RefCell<TreeNode>>>, | ||
p: Option<Rc<RefCell<TreeNode>>>, | ||
q: Option<Rc<RefCell<TreeNode>>>, | ||
) -> Option<Rc<RefCell<TreeNode>>>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
( | ||
&[ | ||
Some(6), | ||
Some(2), | ||
Some(8), | ||
Some(0), | ||
Some(4), | ||
Some(7), | ||
Some(9), | ||
None, | ||
None, | ||
Some(3), | ||
Some(5), | ||
] as &[_], | ||
2, | ||
8, | ||
), | ||
6, | ||
), | ||
( | ||
( | ||
&[ | ||
Some(6), | ||
Some(2), | ||
Some(8), | ||
Some(0), | ||
Some(4), | ||
Some(7), | ||
Some(9), | ||
None, | ||
None, | ||
Some(3), | ||
Some(5), | ||
], | ||
2, | ||
4, | ||
), | ||
2, | ||
), | ||
((&[Some(2), Some(1)], 2, 1), 2), | ||
( | ||
( | ||
&[ | ||
Some(6), | ||
Some(2), | ||
Some(8), | ||
Some(0), | ||
Some(4), | ||
Some(7), | ||
Some(9), | ||
None, | ||
None, | ||
Some(3), | ||
Some(5), | ||
], | ||
3, | ||
5, | ||
), | ||
4, | ||
), | ||
]; | ||
|
||
for ((root, p, q), expected) in test_cases { | ||
let root = test_utilities::make_tree(root.iter().copied()); | ||
let p = test_utilities::find_node(&root, p); | ||
let q = test_utilities::find_node(&root, q); | ||
let expected = test_utilities::find_node(&root, expected); | ||
|
||
assert_eq!(S::lowest_common_ancestor(root, p, q), expected); | ||
} | ||
} | ||
} |