-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode Problem No : 104 -> Maximum Depth of Binary Tree
65 lines (52 loc) · 1.58 KB
/
Leetcode Problem No : 104 -> Maximum Depth of Binary Tree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 🌳 Maximum Depth of Binary Tree (LeetCode #104)
## 📌 Problem Statement
Given the `root` of a binary tree, return its **maximum depth**.
The maximum depth is the number of nodes along the longest path from the root node down to the **furthest leaf node**.
### 🔹 Example 1:
```python
Input: root = [3,9,20,null,null,15,7]
Output: 3
```
```
3
/ \
9 20
/ \
15 7
```
### 🔹 Example 2:
```python
Input: root = [1, null, 2]
Output: 2
```
---
## 🚀 Solution Approach
### 1️⃣ **Depth-First Search (DFS - Recursive):**
- Recursively find the depth of left and right subtrees.
- Maximum depth = `1 + max(left_depth, right_depth)`.
### 2️⃣ **Breadth-First Search (BFS - Iterative):**
- Use a **queue** to traverse level by level.
- Count the number of levels in the tree.
---
## 📝 Python Solution (Recursive - DFS)
```python
# """class Solution:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right"""
class Solution(object):
def maxDepth(self, root):
if root is None:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
---
## ⏳ Complexity Analysis
✅ **Time Complexity:** `O(N)` (Visit all nodes once)
✅ **Space Complexity:**
- **DFS Recursive:** `O(N)` (Recursion stack in worst case)
- **BFS Iterative:** `O(N)` (Queue for level-order traversal)
---
---
## 🔥 Follow for More
If you found this helpful, don't forget to ⭐ the repo and follow me on GitHub! 😊🎉