Skip to content

Commit 7c81db0

Browse files
Problem 104 (#21)
* add solution and resources * update readme
1 parent f1edac8 commit 7c81db0

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
2828
20. [Add Binary #67](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/add-binary-67.md)
2929
21. [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
3030
22. [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
31-
23. Maximum Depth of Binary Tree #104
31+
23. [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
3232
24. Contains Duplicate #217
3333

3434
### Medium
@@ -106,13 +106,15 @@ In order to practice with similar data structures I'll be placing each problem i
106106
- [Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
107107
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
108108
- [Implement Queue using Stacks #232](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/implement-queue-stacks-232.md)
109+
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
109110

110111
### Stack
111112

112113
- [Valid Parentheses #20](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/valid-parentheses-20.md)
113114
- [Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
114115
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
115116
- [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)
116118

117119
### Linked Lists
118120

@@ -139,6 +141,7 @@ In order to practice with similar data structures I'll be placing each problem i
139141
- [Lowest Common Ancestor of a Binary Search Tree #235](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/lowest-common-ancestor-235.md)
140142
- [Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
141143
- [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)
142145

143146
## Algorithm Patterns
144147

@@ -160,6 +163,7 @@ Within the problems above there are several patterns that often occur. I plan to
160163

161164
- [Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
162165
- [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)
163167

164168
### Tree Depth First Search
165169

@@ -168,6 +172,7 @@ Within the problems above there are several patterns that often occur. I plan to
168172
- [Lowest Common Ancestor of a Binary Search Tree #235](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/lowest-common-ancestor-235.md)
169173
- [Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
170174
- [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)
171176

172177
### Divide & Conquer
173178

easy/depth-binary-tree-104.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Maximum Depth of Binary Tree
2+
3+
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+
const maxDepth = function (root) {
47+
if (!root) {
48+
return 0;
49+
}
50+
51+
const depthLeft = maxDepth(root.left);
52+
const depthRight = maxDepth(root.right);
53+
return 1 + 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
60+
61+
```javascript
62+
// Breadth-First Search
63+
const maxDepth = function (root) {
64+
if (!root) {
65+
return 0;
66+
}
67+
68+
const queue = [root];
69+
let depth = 0;
70+
while (queue.length > 0) {
71+
const levelLength = queue.length;
72+
for (let i = 0; i < levelLength; i++) {
73+
const node = queue.shift();
74+
if (node?.left) {
75+
queue.push(node.left);
76+
}
77+
if (node?.right) {
78+
queue.push(node.right);
79+
}
80+
}
81+
depth++;
82+
}
83+
84+
return depth;
85+
};
86+
87+
// Iterative Depth-First Search
88+
const maxDepth = function (root) {
89+
if (!root) {
90+
return 0;
91+
}
92+
93+
const stack = [[root, 1]];
94+
let max = 0;
95+
while (stack.length > 0) {
96+
const [node, depth] = stack.pop();
97+
max = Math.max(max, depth);
98+
99+
if (node?.left) {
100+
stack.push([node.left, depth + 1]);
101+
}
102+
if (node?.right) {
103+
stack.push([node.right, depth + 1]);
104+
}
105+
}
106+
107+
return max;
108+
};
109+
```

0 commit comments

Comments
 (0)