-
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 2331: Evaluate Boolean Binary Tree
- Loading branch information
Showing
3 changed files
with
185 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,135 @@ | ||
use crate::data_structures::TreeNode; | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
pub mod recursive; | ||
|
||
pub trait Solution { | ||
fn evaluate_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
type TestCase<'a> = (&'a [Option<i32>], bool); | ||
|
||
const EXTRA_TEST_CASE_1: TestCase = ( | ||
&[ | ||
Some(3), | ||
Some(3), | ||
Some(2), | ||
Some(0), | ||
Some(0), | ||
Some(3), | ||
Some(2), | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(1), | ||
Some(3), | ||
Some(1), | ||
Some(1), | ||
None, | ||
None, | ||
Some(2), | ||
Some(1), | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(1), | ||
Some(1), | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
], | ||
false, | ||
); | ||
|
||
const EXTRA_TEST_CASE_2: TestCase = ( | ||
&[ | ||
Some(3), | ||
Some(2), | ||
Some(3), | ||
Some(3), | ||
Some(3), | ||
Some(3), | ||
Some(3), | ||
Some(2), | ||
Some(2), | ||
Some(2), | ||
Some(1), | ||
Some(2), | ||
Some(0), | ||
Some(0), | ||
Some(3), | ||
Some(0), | ||
Some(2), | ||
Some(0), | ||
Some(1), | ||
Some(0), | ||
Some(0), | ||
None, | ||
None, | ||
Some(0), | ||
Some(1), | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(1), | ||
Some(1), | ||
None, | ||
None, | ||
Some(3), | ||
Some(0), | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(1), | ||
Some(0), | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
], | ||
false, | ||
); | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[Some(2), Some(1), Some(3), None, None, Some(0), Some(1)] as &[_], true), | ||
(&[Some(0)], false), | ||
EXTRA_TEST_CASE_1, | ||
EXTRA_TEST_CASE_2, | ||
]; | ||
|
||
for (root, expected) in test_cases { | ||
assert_eq!( | ||
S::evaluate_tree(test_utilities::make_tree(root.iter().copied())), | ||
expected, | ||
); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/problem_2331_evaluate_boolean_binary_tree/recursive.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; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
impl Solution { | ||
fn helper(node: &Option<Rc<RefCell<TreeNode>>>) -> bool { | ||
let node = node.as_deref().unwrap().borrow(); | ||
let node = &*node; | ||
|
||
match node.val { | ||
0 => false, | ||
1 => true, | ||
operator => { | ||
let lhs = Self::helper(&node.left); | ||
|
||
if operator == 2 { | ||
lhs || Self::helper(&node.right) | ||
} else { | ||
lhs && Self::helper(&node.right) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn evaluate_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool { | ||
Self::helper(&root) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn evaluate_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool { | ||
Self::evaluate_tree(root) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |