Skip to main content
Back to problems
#671
Easy Algorithms

Second minimum node in a binary tree

Tree Depth-First Search Binary Tree
46.0% acceptance
Feb 20, 2026
2001
1902
Given a special binary tree where root.val = min(left.val, right.val), return the second minimum value, or -1 if it doesn't exist.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
  pub fn find_second_minimum_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
    fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, min_val: i32) -> i64 {
      match node {
        None => i64::MAX,
        Some(n) => {
          let b = n.borrow();
          if b.val > min_val {
            return b.val as i64;
          }
          let left = dfs(&b.left, min_val);
          let right = dfs(&b.right, min_val);
          left.min(right)
        }
      }
    }
    if root.is_none() { return -1; }
    let min_val = root.as_ref().unwrap().borrow().val;
    let res = dfs(&root, min_val);
    if res == i64::MAX { -1 } else { res as i32 }
  }
}