|
| 1 | +--- |
| 2 | +id: max-depth-binary-tree |
| 3 | +title: Maximum Depth of Binary Tree Solution |
| 4 | +sidebar_label: 0104 Maximum Depth of Binary Tree |
| 5 | +tags: |
| 6 | + - Binary Tree |
| 7 | + - Depth-First Search |
| 8 | + - Breadth-First Search |
| 9 | + - Recursion |
| 10 | + - LeetCode |
| 11 | + - C++ |
| 12 | + - Java |
| 13 | + - Python |
| 14 | +description: "This is a solution to the Maximum Depth of Binary Tree problem on LeetCode." |
| 15 | +--- |
| 16 | + |
| 17 | +## Problem Description |
| 18 | + |
| 19 | +Given the root of a binary tree, return its maximum depth. |
| 20 | + |
| 21 | +A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. |
| 22 | + |
| 23 | +### Examples |
| 24 | + |
| 25 | +**Example 1:** |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +``` |
| 30 | +Input: root = [3,9,20,null,null,15,7] |
| 31 | +Output: 3 |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +``` |
| 37 | +Input: root = [1,null,2] |
| 38 | +Output: 2 |
| 39 | +``` |
| 40 | + |
| 41 | +### Constraints |
| 42 | + |
| 43 | +- The number of nodes in the tree is in the range `[0, 104]`. |
| 44 | +- `-100 <= Node.val <= 100` |
| 45 | + |
| 46 | +## Solution for Maximum Depth of Binary Tree Problem |
| 47 | + |
| 48 | + |
| 49 | +<Tabs> |
| 50 | + <TabItem value="Recursive" label="Recursive"> |
| 51 | + |
| 52 | +### Recursive (DFS) Approach |
| 53 | + |
| 54 | +#### Intuition |
| 55 | + |
| 56 | +To find the maximum depth of a binary tree, we can use a depth-first search (DFS) approach. We recursively find the maximum depth of the left and right subtrees and return the maximum of the two depths plus one (to account for the current level). |
| 57 | + |
| 58 | +#### Approach |
| 59 | + |
| 60 | +1. Implement a recursive function `maxDepth(root)` that returns the maximum depth of the binary tree rooted at `root`. |
| 61 | +2. If the root is `null`, return 0. |
| 62 | +3. Recursively find the maximum depth of the left and right subtrees. |
| 63 | +4. Return the maximum of the left and right subtree depths plus one. |
| 64 | + |
| 65 | +#### Code in Different Languages |
| 66 | + |
| 67 | +<Tabs> |
| 68 | + <TabItem value="Java" label="Java" default> |
| 69 | + <SolutionAuthor name="@Vipullakum007"/> |
| 70 | + ```java |
| 71 | + public int maxDepth(TreeNode root) { |
| 72 | + if (root == null) |
| 73 | + return 0; |
| 74 | + int maxLeft = maxDepth(root.left); |
| 75 | + int maxRight = maxDepth(root.right); |
| 76 | + return Math.max(maxLeft, maxRight) + 1; |
| 77 | + } |
| 78 | + ``` |
| 79 | + |
| 80 | + </TabItem> |
| 81 | + <TabItem value="Python" label="Python"> |
| 82 | + <SolutionAuthor name="@Vipullakum007"/> |
| 83 | + ```python |
| 84 | + def maxDepth(self, root: TreeNode) -> int: |
| 85 | + if root is None: |
| 86 | + return 0 |
| 87 | + maxLeft = self.maxDepth(root.left) |
| 88 | + maxRight = self.maxDepth(root.right) |
| 89 | + return max(maxLeft, maxRight) + 1 |
| 90 | + ``` |
| 91 | + |
| 92 | + </TabItem> |
| 93 | + <TabItem value="C++" label="C++"> |
| 94 | + <SolutionAuthor name="@Vipullakum007"/> |
| 95 | + ```cpp |
| 96 | + int maxDepth(TreeNode* root) { |
| 97 | + if (!root) |
| 98 | + return 0; |
| 99 | + int maxLeft = maxDepth(root->left); |
| 100 | + int maxRight = maxDepth(root->right); |
| 101 | + return max(maxLeft, maxRight) + 1; |
| 102 | + } |
| 103 | + ``` |
| 104 | + |
| 105 | + </TabItem> |
| 106 | +</Tabs> |
| 107 | + |
| 108 | +#### Complexity Analysis |
| 109 | + |
| 110 | +- **Time complexity**: O(n), where n is the number of nodes in the tree. We visit each node once. |
| 111 | +- **Space complexity**: O(h), where h is the height of the tree. The space used by the recursive call stack is proportional to the height of the tree. |
| 112 | + |
| 113 | +</TabItem> |
| 114 | +<TabItem value="BFS" label="BFS" default> |
| 115 | + |
| 116 | +### Breadth-First Search (BFS) Approach |
| 117 | + |
| 118 | +#### Intuition |
| 119 | + |
| 120 | +Another approach to find the maximum depth of a binary tree is to use breadth-first search (BFS). We start from the root node and traverse the tree level by level, incrementing the depth at each level until we reach the deepest leaf node. |
| 121 | + |
| 122 | +#### Approach |
| 123 | + |
| 124 | +1. Initialize a queue and push the root node into it. |
| 125 | +2. Initialize a variable `depth` to store the maximum depth, initially set to 0. |
| 126 | +3. Perform BFS traversal: |
| 127 | + - Increment `depth` for each level visited. |
| 128 | + - Push all the children of the current level into the queue. |
| 129 | +4. Return `depth` after traversing all levels. |
| 130 | + |
| 131 | +#### Code in Different Languages |
| 132 | + |
| 133 | +<Tabs> |
| 134 | + <TabItem value="Java" label="Java" default> |
| 135 | + <SolutionAuthor name="@Vipullakum007"/> |
| 136 | + ```java |
| 137 | + public int maxDepth(TreeNode root) { |
| 138 | + if (root == null) |
| 139 | + return 0; |
| 140 | + |
| 141 | + int depth = 0; |
| 142 | + Queue<TreeNode> queue = new LinkedList<>(); |
| 143 | + queue.offer(root); |
| 144 | + while (!queue.isEmpty()) { |
| 145 | + ++depth; |
| 146 | + int levelSize = queue.size(); |
| 147 | + for (int i = 0; i < levelSize; ++i) { |
| 148 | + TreeNode node = queue.poll(); |
| 149 | + if (node.left != null) |
| 150 | + queue.offer(node.left); |
| 151 | + if (node.right != null) |
| 152 | + queue.offer(node.right); |
| 153 | + } |
| 154 | + } |
| 155 | + return depth; |
| 156 | + } |
| 157 | + ``` |
| 158 | + |
| 159 | + </TabItem> |
| 160 | + <TabItem value="Python" label="Python"> |
| 161 | + <SolutionAuthor name="@Vipullakum007"/> |
| 162 | + ```python |
| 163 | + def maxDepth(self, root: TreeNode) -> int: |
| 164 | + if root is None: |
| 165 | + return 0 |
| 166 | + |
| 167 | + depth = 0 |
| 168 | + queue = [root] |
| 169 | + while queue: |
| 170 | + depth += 1 |
| 171 | + for _ in range(len(queue)): |
| 172 | + node = queue.pop(0) |
| 173 | + if node.left: |
| 174 | + queue.append(node.left) |
| 175 | + if node.right: |
| 176 | + queue.append(node.right) |
| 177 | + return depth |
| 178 | + ``` |
| 179 | + |
| 180 | + </TabItem> |
| 181 | + <TabItem value="C++" label="C++"> |
| 182 | + <SolutionAuthor name="@Vipullakum007"/> |
| 183 | + ```cpp |
| 184 | + int maxDepth(TreeNode *root) { |
| 185 | + if (root == NULL) |
| 186 | + return 0; |
| 187 | + |
| 188 | + int depth = 0; |
| 189 | + queue<TreeNode *> q; |
| 190 | + q.push(root); |
| 191 | + while (!q.empty()) { |
| 192 | + ++depth; |
| 193 | + int levelSize = q.size(); |
| 194 | + for (int i = 0; i < levelSize; ++i) { |
| 195 | + TreeNode *node = q.front(); |
| 196 | + q.pop(); |
| 197 | + if (node->left) |
| 198 | + q.push(node->left); |
| 199 | + if (node->right) |
| 200 | + q.push(node->right); |
| 201 | + } |
| 202 | + } |
| 203 | + return depth; |
| 204 | + } |
| 205 | + ``` |
| 206 | + |
| 207 | + </TabItem> |
| 208 | +</Tabs> |
| 209 | + |
| 210 | +#### Complexity Analysis |
| 211 | + |
| 212 | +- **Time complexity**: O(n), where n is the number of nodes in the tree. We visit each node once. |
| 213 | +- **Space complexity**: O(w), where w is the maximum width of the tree (i.e., the number of nodes at the maximum level). In the worst case, the maximum width could be n/2 in a complete binary tree, so the space complexity is O(n). |
| 214 | + |
| 215 | +</TabItem> |
| 216 | +</Tabs> |
| 217 | + |
| 218 | +## References |
| 219 | + |
| 220 | +- **LeetCode Problem**: [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) |
| 221 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/maximum-depth-of-binary-tree/solution/) |
| 222 | +- **Authors GeeksforGeeks Profile:** [Vipul lakum](https://leetcode.com/u/vipul_lakum_02/) |
| 223 | +--- |
0 commit comments