#111
Easy Algorithms Minimum depth of binary tree
Tree Depth-First Search Breadth-First Search Binary Tree
52.4% acceptance
Feb 27, 2026
7810
1365
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Solution
Rust
Time O(n)
Space O(n)
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
use std::collections::VecDeque;
let Some(root_node) = root else { return 0; };
let mut queue = VecDeque::new();
queue.push_back((root_node, 1));
while let Some((node, depth)) = queue.pop_front() {
let node_borrow = node.borrow();
if node_borrow.left.is_none() && node_borrow.right.is_none() {
return depth;
}
if let Some(left) = &node_borrow.left {
queue.push_back((Rc::clone(left), depth + 1));
}
if let Some(right) = &node_borrow.right {
queue.push_back((Rc::clone(right), depth + 1));
}
}
0
}
}