You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
114
115
-[Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
115
116
-[Implement Queue using Stacks #232](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/implement-queue-stacks-232.md)
117
+
-[Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
116
118
117
119
### Linked Lists
118
120
@@ -139,6 +141,7 @@ In order to practice with similar data structures I'll be placing each problem i
139
141
-[Lowest Common Ancestor of a Binary Search Tree #235](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/lowest-common-ancestor-235.md)
140
142
-[Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
141
143
-[Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
144
+
-[Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
142
145
143
146
## Algorithm Patterns
144
147
@@ -160,6 +163,7 @@ Within the problems above there are several patterns that often occur. I plan to
160
163
161
164
-[Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
162
165
-[Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
166
+
-[Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
163
167
164
168
### Tree Depth First Search
165
169
@@ -168,6 +172,7 @@ Within the problems above there are several patterns that often occur. I plan to
168
172
-[Lowest Common Ancestor of a Binary Search Tree #235](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/lowest-common-ancestor-235.md)
169
173
-[Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
170
174
-[Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
175
+
-[Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
Page on leetcode: https://leetcode.com/problems/maximum-depth-of-binary-tree/
4
+
5
+
## Problem Statement
6
+
7
+
Given the root of a binary tree, return its maximum depth. 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.
8
+
9
+
### Constraints
10
+
11
+
- The number of nodes in the tree is in the range [0, 104].
12
+
- -100 <= Node.val <= 100
13
+
14
+
### Example
15
+
16
+
```
17
+
Input: root = [3,9,20,null,null,15,7]
18
+
Output: 3
19
+
```
20
+
21
+
```
22
+
Input: root = [1,null,2]
23
+
Output: 2
24
+
```
25
+
26
+
## Solution
27
+
28
+
Either DFS or BFS for traversal, I would select DFS. Recursion seems like the simplest way to approach. Edge cases would be null root or entire left branch null.
29
+
30
+
### Initial Pseudocode
31
+
32
+
1. Create max variable
33
+
2. Create recursive function
34
+
3. if node is null return 0
35
+
4. Create a count variable equal to a recursive call
36
+
5. Set max equal to max(max, count)
37
+
6. return 1
38
+
7. Call recursive function
39
+
8. return max
40
+
41
+
### Initial Solution
42
+
43
+
This solution has a time and space complexity of O(n).
44
+
45
+
```javascript
46
+
constmaxDepth=function (root) {
47
+
if (!root) {
48
+
return0;
49
+
}
50
+
51
+
constdepthLeft=maxDepth(root.left);
52
+
constdepthRight=maxDepth(root.right);
53
+
return1+Math.max(depthLeft, depthRight);
54
+
};
55
+
```
56
+
57
+
### Optimized Solution
58
+
59
+
These alternative solutions have the same time and space complexity as above. You can see an explanation of the solutions here: https://www.youtube.com/watch?v=hTM3phVI6YQ
0 commit comments