Skip to content

Commit

Permalink
Add problem 2331: Evaluate Boolean Binary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 21, 2024
1 parent 98e637b commit b27d1ff
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,7 @@ pub mod problem_2320_count_number_of_ways_to_place_houses;
pub mod problem_2321_maximum_score_of_spliced_array;
pub mod problem_2325_decode_the_message;
pub mod problem_2326_spiral_matrix_iv;
pub mod problem_2331_evaluate_boolean_binary_tree;

#[cfg(test)]
mod test_utilities;
135 changes: 135 additions & 0 deletions src/problem_2331_evaluate_boolean_binary_tree/mod.rs
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 src/problem_2331_evaluate_boolean_binary_tree/recursive.rs
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>();
}
}

0 comments on commit b27d1ff

Please sign in to comment.