Skip to main content
Back to problems
#2331
Easy Algorithms

Evaluate boolean binary tree

Tree Depth-First Search Binary Tree
82.4% acceptance
Feb 25, 2026
1546
44
You are given the root of a full binary tree: Leaf nodes have either the value 0 (False) or 1 (True). Non-leaf nodes have either the value 2 (OR) or 3 (AND). Return the boolean result of evaluating the root node.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
  pub fn evaluate_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
    let node = root.unwrap();
    let borrowed = node.borrow();
    let val = borrowed.val;
    if borrowed.left.is_none() {
      return val == 1;
    }
    let left = Self::evaluate_tree(borrowed.left.clone());
    let right = Self::evaluate_tree(borrowed.right.clone());
    if val == 2 { left || right } else { left && right }
  }
}