Skip to content

Commit dfeada7

Browse files
committed
'Minimum Depth of Binary Tree' soln
1 parent 1fff326 commit dfeada7

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ Note: Some solutions have multiple approaches implemented
116116
| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Solution](./leetcode/convert_sorted_array_to_bst.rs) | Easy |
117117
| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Solution](./leetcode/convert_sorted_list_to_bst.rs) | Medium |
118118
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Solution](./leetcode/balanced_binary_tree.rs) | Easy |
119-
| >111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](./leetcode/minimum_depth_of_binary_tree.rs) | Easy |
120-
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Solution](./leetcode/path_sum.rs) | Easy |
119+
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](./leetcode/minimum_depth_of_binary_tree.rs) | Easy |
120+
| >112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Solution](./leetcode/path_sum.rs) | Easy |
121121
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [Solution](./leetcode/path_sum_ii.rs) | Medium |
122122
| 114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](./leetcode/flatten_binary_tree.rs) | Medium |
123123
| 115 | [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) | [Solution](./leetcode/distinct_subsequences.rs) | Hard |
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
///
2+
/// Problem: 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
7+
/// down to the nearest leaf node.
8+
///
9+
/// Note: A leaf is a node with no children.
10+
///
11+
/// Example 1:
12+
/// Input: root = [3,9,20,null,null,15,7]
13+
/// Output: 2
14+
///
15+
/// Example 2:
16+
/// Input: root = [2,null,3,null,4,null,5,null,6]
17+
/// Output: 5
18+
///
19+
/// Constraints:
20+
/// The number of nodes in the tree is in the range [0, 10^5].
21+
/// -1000 <= Node.val <= 1000
22+
///
23+
24+
// Definition for a binary tree node (provided by LeetCode)
25+
// #[derive(Debug, PartialEq, Eq)]
26+
// pub struct TreeNode {
27+
// pub val: i32,
28+
// pub left: Option<Rc<RefCell<TreeNode>>>,
29+
// pub right: Option<Rc<RefCell<TreeNode>>>,
30+
// }
31+
//
32+
// impl TreeNode {
33+
// #[inline]
34+
// pub fn new(val: i32) -> Self {
35+
// TreeNode {
36+
// val,
37+
// left: None,
38+
// right: None
39+
// }
40+
// }
41+
// }
42+
43+
// # Solution
44+
// Time complexity: O(n)
45+
// Space complexity: O(h) Where h is the height of the tree (for the recursion stack)
46+
47+
48+
use std::rc::Rc;
49+
use std::cell::RefCell;
50+
use std::cmp;
51+
impl Solution {
52+
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
53+
match root {
54+
None => 0,
55+
Some(node) => {
56+
let node_ref = node.borrow();
57+
let left_depth = Self::min_depth(node_ref.left.clone());
58+
let right_depth = Self::min_depth(node_ref.right.clone());
59+
60+
61+
if left_depth == 0 {
62+
return right_depth + 1;
63+
}
64+
if right_depth == 0 {
65+
return left_depth + 1;
66+
}
67+
68+
69+
1 + cmp::min(left_depth, right_depth)
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)