Skip to content

Commit 158eca3

Browse files
committed
solve #111
1 parent 93570ec commit 158eca3

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,4 @@ mod n0107_binary_tree_level_order_traversal_ii;
111111
mod n0108_convert_sorted_array_to_binary_search_tree;
112112
mod n0109_convert_sorted_list_to_binary_search_tree;
113113
mod n0110_balanced_binary_tree;
114+
mod n0111_minimum_depth_of_binary_tree;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* [111] Minimum Depth of Binary Tree
3+
*
4+
* Given a binary tree, find its minimum depth.
5+
*
6+
* The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
7+
*
8+
* Note: A leaf is a node with no children.
9+
*
10+
* Example:
11+
*
12+
* Given binary tree [3,9,20,null,null,15,7],
13+
*
14+
*
15+
* 3
16+
* / \
17+
* 9 20
18+
* / \
19+
* 15 7
20+
*
21+
* return its minimum depth = 2.
22+
*
23+
*/
24+
pub struct Solution {}
25+
use super::util::tree::{TreeNode, to_tree};
26+
27+
// submission codes start here
28+
29+
use std::rc::Rc;
30+
use std::cell::RefCell;
31+
use std::collections::VecDeque;
32+
impl Solution {
33+
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
34+
if root.is_none() { return 0 }
35+
let mut deq = VecDeque::new();
36+
deq.push_back((1, root.clone()));
37+
while !deq.is_empty() {
38+
if let Some((level, Some(node))) = deq.pop_front() {
39+
if node.borrow().left.is_none() && node.borrow().right.is_none() {
40+
return level
41+
}
42+
deq.push_back((level+1, node.borrow().left.clone()));
43+
deq.push_back((level+1, node.borrow().right.clone()));
44+
}
45+
}
46+
0
47+
}
48+
}
49+
50+
// submission codes end
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
fn test_111() {
58+
assert_eq!(Solution::min_depth(tree![3,9,20,null,null,15,7]), 2);
59+
}
60+
}

0 commit comments

Comments
 (0)