|
| 1 | +<h1 align='center'>Maximum - Depth - Of - Binary - Tree</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Problem Explanation |
| 10 | +The problem is to find the maximum depth, or height, of a binary tree. In a binary tree, the depth is the length of the longest path from the root node to any leaf node. |
| 11 | + |
| 12 | +The depth of the tree is: |
| 13 | +- **0** for an empty tree. |
| 14 | +- **1** for a tree with only the root node. |
| 15 | +- **Increased by one** for each additional level from root to the furthest leaf node. |
| 16 | + |
| 17 | +#### Examples: |
| 18 | +1. **Example 1**: |
| 19 | + ``` |
| 20 | + 1 |
| 21 | + / \ |
| 22 | + 2 3 |
| 23 | + / |
| 24 | + 4 |
| 25 | + ``` |
| 26 | + - The maximum depth is `3`, as the longest path (1 → 2 → 4) has three edges. |
| 27 | + |
| 28 | +2. **Example 2**: |
| 29 | + ``` |
| 30 | + 1 |
| 31 | + ``` |
| 32 | + - This tree has only the root, so the depth is `1`. |
| 33 | + |
| 34 | +3. **Example 3**: An empty tree |
| 35 | + - If the tree is empty (`root == NULL`), the depth is `0`. |
| 36 | + |
| 37 | +### Step 2: Approach to Solve the Problem |
| 38 | + |
| 39 | +#### Think Like a Beginner: |
| 40 | + |
| 41 | +1. **Using Level-by-Level Traversal (Breadth-First Search - BFS)**: |
| 42 | + - We can determine the tree’s depth by traversing it level-by-level. |
| 43 | + - At each level, count the number of nodes and increase the depth by `1`. |
| 44 | + - Use a queue to keep track of nodes level-by-level. |
| 45 | + |
| 46 | +2. **Tracking Each Level**: |
| 47 | + - Start with the root node. |
| 48 | + - For each level, count how many nodes there are. This count tells us how many nodes to process at the current depth. |
| 49 | + - As each node’s children are added to the queue, they become part of the next level. |
| 50 | + |
| 51 | +3. **Termination**: |
| 52 | + - When all nodes are processed, the total depth will represent the maximum depth. |
| 53 | + |
| 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 maxDepth(TreeNode* root) { |
| 71 | + if(root == NULL) return 0; |
| 72 | + |
| 73 | + queue<TreeNode*> q; |
| 74 | + q.push(root); |
| 75 | + int depth = 0; |
| 76 | + |
| 77 | + while(!q.empty()){ |
| 78 | + int levelSize = q.size(); |
| 79 | + depth++; |
| 80 | + |
| 81 | + for(int i = 0; i < levelSize; i++){ |
| 82 | + TreeNode* temp = q.front(); |
| 83 | + q.pop(); |
| 84 | + |
| 85 | + if(temp -> left) q.push(temp -> left); |
| 86 | + if(temp -> right) q.push(temp -> right); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return depth; |
| 91 | + } |
| 92 | +}; |
| 93 | +``` |
| 94 | + |
| 95 | +## Problem Solution Explanation |
| 96 | +Here’s how the code finds the maximum depth: |
| 97 | + |
| 98 | +```cpp |
| 99 | +class Solution { |
| 100 | +public: |
| 101 | + int maxDepth(TreeNode* root) { |
| 102 | + if(root == NULL) return 0; |
| 103 | +``` |
| 104 | +- **Explanation**: This code checks if the root is `NULL`. If so, it means the tree is empty, and the function immediately returns `0` (depth is zero). |
| 105 | +
|
| 106 | +```cpp |
| 107 | + queue<TreeNode*> q; |
| 108 | + q.push(root); |
| 109 | + int depth = 0; |
| 110 | +``` |
| 111 | +- **Explanation**: We create a queue `q` to keep track of nodes as we process each level. The root node is added first, and `depth` is initialized to `0`. |
| 112 | + |
| 113 | +```cpp |
| 114 | + while(!q.empty()) { |
| 115 | + int levelSize = q.size(); |
| 116 | + depth++; |
| 117 | +``` |
| 118 | +- **Explanation**: As long as the queue isn’t empty, we’re still processing levels. Each level starts by getting the `levelSize`, which represents the number of nodes in the current level. `depth` is incremented each time we start a new level. |
| 119 | +
|
| 120 | +```cpp |
| 121 | + for(int i = 0; i < levelSize; i++) { |
| 122 | + TreeNode* temp = q.front(); |
| 123 | + q.pop(); |
| 124 | +``` |
| 125 | +- **Explanation**: The `for` loop goes through all nodes at the current level (`levelSize` times). `temp` points to each node in the queue, one by one, and removes it from the queue. |
| 126 | + |
| 127 | +```cpp |
| 128 | + if(temp -> left) q.push(temp -> left); |
| 129 | + if(temp -> right) q.push(temp -> right); |
| 130 | + } |
| 131 | + } |
| 132 | +``` |
| 133 | +- **Explanation**: For each node, if there’s a left child, it’s added to the queue; similarly, if there’s a right child, it’s added to the queue. This process adds nodes of the next level to the queue, so they’re ready for processing in the next iteration of the `while` loop. |
| 134 | + |
| 135 | +```cpp |
| 136 | + return depth; |
| 137 | + } |
| 138 | +}; |
| 139 | +``` |
| 140 | +- **Explanation**: When the queue is empty, all levels have been processed. `depth` now holds the maximum depth of the binary tree, and it’s returned as the result. |
| 141 | + |
| 142 | +### Example Walkthrough |
| 143 | + |
| 144 | +For the tree: |
| 145 | + |
| 146 | +``` |
| 147 | + 1 |
| 148 | + / \ |
| 149 | + 2 3 |
| 150 | + / |
| 151 | + 4 |
| 152 | +``` |
| 153 | + |
| 154 | +1. **Initial Queue**: `[1]` |
| 155 | + - Depth = 0 |
| 156 | + |
| 157 | +2. **First Level**: `[1]` → Depth = 1 |
| 158 | + - `1` is removed, `2` and `3` are added to the queue: `[2, 3]` |
| 159 | + |
| 160 | +3. **Second Level**: `[2, 3]` → Depth = 2 |
| 161 | + - `2` is removed, `4` is added; `3` is removed (no children) |
| 162 | + - Queue after this level: `[4]` |
| 163 | + |
| 164 | +4. **Third Level**: `[4]` → Depth = 3 |
| 165 | + - `4` is removed (no children), queue is empty. |
| 166 | + |
| 167 | +The final depth is `3`, so the maximum depth is `3`. |
| 168 | + |
| 169 | +### Time and Space Complexity |
| 170 | + |
| 171 | +1. **Time Complexity**: **O(N)**, where `N` is the number of nodes in the tree, because each node is processed once. |
| 172 | +2. **Space Complexity**: **O(N)** for the queue in the worst case, if the last level has `N/2` nodes. |
0 commit comments