|
| 1 | +<h1 align='center'>Minimum - Depth - of - Binary - Tree</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Problem Explanation |
| 10 | +The goal of this problem is to find the **minimum depth** of a binary tree. |
| 11 | + |
| 12 | +The minimum depth is defined as the shortest path from the root node down to the nearest leaf node. A **leaf node** is a node that has no children (both left and right child nodes are `NULL`). This is different from the maximum depth, which considers the longest path. |
| 13 | + |
| 14 | +#### Examples: |
| 15 | +1. **Example 1**: |
| 16 | + ``` |
| 17 | + 1 |
| 18 | + / \ |
| 19 | + 2 3 |
| 20 | + / |
| 21 | + 4 |
| 22 | + ``` |
| 23 | + - The minimum depth here is `2`, as the shortest path is from the root `1` to node `3`. |
| 24 | + |
| 25 | +2. **Example 2**: |
| 26 | + ``` |
| 27 | + 1 |
| 28 | + / |
| 29 | + 2 |
| 30 | + / |
| 31 | + 3 |
| 32 | + ``` |
| 33 | + - The minimum depth here is `3`, as there's only one path from the root to a leaf node (1 → 2 → 3). |
| 34 | + |
| 35 | +3. **Example 3**: An empty tree |
| 36 | + - If the tree is empty (`root == NULL`), the depth is `0`. |
| 37 | + |
| 38 | +### Step 2: Approach to Solve the Problem |
| 39 | + |
| 40 | +#### Beginner-Friendly Thinking Process: |
| 41 | + |
| 42 | +1. **Recursion to Simplify Tree Traversal**: |
| 43 | + - If we view the problem recursively, finding the minimum depth can be broken down into smaller subproblems for each subtree (left and right). |
| 44 | + - At each node, the minimum depth is 1 + the minimum depth of its non-null child subtree. |
| 45 | + |
| 46 | +2. **Handling Edge Cases with NULL Nodes**: |
| 47 | + - If a node has no children on one side (i.e., either left or right is `NULL`), the minimum depth should only consider the non-null child. |
| 48 | + - For a node with both children, find the minimum depth of both subtrees and add 1 for the current node. |
| 49 | + |
| 50 | +3. **Recursive Steps**: |
| 51 | + - If both children are `NULL`, it’s a leaf node, and the depth from this node is 1. |
| 52 | + - If only one child is `NULL`, continue down the subtree with the non-null child. |
| 53 | + - If both children are present, calculate the minimum depth of both left and right subtrees, then add 1 to account for the current node. |
| 54 | + |
| 55 | +## Problem Solution |
| 56 | +```cpp |
| 57 | +/** |
| 58 | + * Definition for a binary tree node. |
| 59 | + * struct TreeNode { |
| 60 | + * int val; |
| 61 | + * TreeNode *left; |
| 62 | + * TreeNode *right; |
| 63 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 64 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 65 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 66 | + * }; |
| 67 | + */ |
| 68 | +class Solution { |
| 69 | +public: |
| 70 | + int minDepth(TreeNode* root) { |
| 71 | + if(root == NULL) return 0; |
| 72 | + |
| 73 | + if(root -> left == NULL) return minDepth(root -> right) + 1; |
| 74 | + if(root -> right == NULL) return minDepth(root -> left) + 1; |
| 75 | + |
| 76 | + int left = minDepth(root -> left); |
| 77 | + int right = minDepth(root -> right); |
| 78 | + |
| 79 | + return min(left, right) + 1; |
| 80 | + } |
| 81 | +}; |
| 82 | +``` |
| 83 | + |
| 84 | +## Problem Solution Explanation |
| 85 | + |
| 86 | +```cpp |
| 87 | +class Solution { |
| 88 | +public: |
| 89 | + int minDepth(TreeNode* root) { |
| 90 | + if(root == NULL) return 0; |
| 91 | +``` |
| 92 | +- **Explanation**: If `root` is `NULL`, the tree is empty, so return `0`. |
| 93 | +
|
| 94 | +```cpp |
| 95 | + if(root -> left == NULL) return minDepth(root -> right) + 1; |
| 96 | + if(root -> right == NULL) return minDepth(root -> left) + 1; |
| 97 | +``` |
| 98 | +- **Explanation**: If there is no left child (`root->left == NULL`), the minimum depth must come from the right subtree, so call `minDepth` on `root->right` and add 1 for the current node. |
| 99 | +- Similarly, if there is no right child (`root->right == NULL`), the minimum depth must come from the left subtree. |
| 100 | + |
| 101 | +```cpp |
| 102 | + int left = minDepth(root -> left); |
| 103 | + int right = minDepth(root -> right); |
| 104 | +``` |
| 105 | +- **Explanation**: If both children exist, calculate the depth of the left and right subtrees separately. |
| 106 | + |
| 107 | +```cpp |
| 108 | + return min(left, right) + 1; |
| 109 | + } |
| 110 | +}; |
| 111 | +``` |
| 112 | +- **Explanation**: Return the minimum of the left and right depths plus 1 for the current node. |
| 113 | + |
| 114 | +### Step-by-Step Example Walkthrough |
| 115 | + |
| 116 | +For the following tree: |
| 117 | + |
| 118 | +``` |
| 119 | + 1 |
| 120 | + / \ |
| 121 | + 2 3 |
| 122 | + / |
| 123 | + 4 |
| 124 | +``` |
| 125 | + |
| 126 | +1. **Node 1**: It has both children, so find the minimum depth of both left and right subtrees. |
| 127 | +2. **Left Subtree of 1**: |
| 128 | + - Node `2` has only a left child (node `4`), so continue to node `4`. |
| 129 | + - Node `4` has no children, so its depth is `1`. |
| 130 | + - Depth of left subtree from node `1` is `2` (1 for `2` and 1 for `4`). |
| 131 | +3. **Right Subtree of 1**: |
| 132 | + - Node `3` has no children, so its depth is `1`. |
| 133 | +4. **Result**: The minimum depth from root `1` is `2` (shortest path being 1 → 3). |
| 134 | + |
| 135 | +### Step 4: Time and Space Complexity |
| 136 | + |
| 137 | +1. **Time Complexity**: **O(N)**, where `N` is the number of nodes in the tree. Each node is visited once, so the time grows linearly with the number of nodes. |
| 138 | +2. **Space Complexity**: **O(H)**, where `H` is the height of the tree. This space is required for the recursive call stack. In the worst case, the height could be `N` (for a skewed tree), and in the best case, it’s `log(N)` for a balanced tree. |
0 commit comments