Skip to content

Commit 16d8014

Browse files
Problem 543 (#19)
* add solution and resources * update README
1 parent 20e22db commit 16d8014

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
2626
18. [Reverse Linked List #206](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/reverse-linked-list-206.md)
2727
19. [Majority Element #169](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/majority-element-169.md)
2828
20. [Add Binary #67](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/add-binary-67.md)
29-
21. Diameter of Binary Tree #543
29+
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
3131
23. Maximum Depth of Binary Tree #104
3232
24. Contains Duplicate #217
@@ -137,6 +137,7 @@ In order to practice with similar data structures I'll be placing each problem i
137137
- [Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
138138
- [Lowest Common Ancestor of a Binary Search Tree #235](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/lowest-common-ancestor-235.md)
139139
- [Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
140+
- [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
140141

141142
## Algorithm Patterns
142143

@@ -164,6 +165,7 @@ Within the problems above there are several patterns that often occur. I plan to
164165
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
165166
- [Lowest Common Ancestor of a Binary Search Tree #235](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/lowest-common-ancestor-235.md)
166167
- [Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
168+
- [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
167169

168170
### Divide & Conquer
169171

easy/diameter-binary-tree-543.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Diameter of Binary Tree
2+
3+
Page on leetcode:https://leetcode.com/problems/diameter-of-binary-tree/
4+
5+
## Problem Statement
6+
7+
Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. The length of a path between two nodes is represented by the number of edges between them.
8+
9+
### Constraints
10+
11+
- The number of nodes in the tree is in the range [1, 104].
12+
- -100 <= Node.val <= 100
13+
14+
### Example
15+
16+
```
17+
Input: root = [1,2,3,4,5]
18+
Output: 3
19+
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
20+
```
21+
22+
## Solution
23+
24+
Since we only need to return the length we can probably use a max variable. DFS seems like a decent approach.
25+
26+
### Pseudocode
27+
28+
1. Set max variable to 0
29+
2. create dfs helper function
30+
3. check if root is null, if true return 0
31+
4. recursive check on left and right, add one for each call
32+
5. set max to the max(max, left + right)
33+
6. return max
34+
35+
### Initial Attempt
36+
37+
```javascript
38+
const diameterOfBinaryTree = function (root) {
39+
let max = 0;
40+
41+
function dfs(root) {
42+
if (!root) {
43+
return 0;
44+
}
45+
const left = 1 + dfs(root.left);
46+
const right = 1 + dfs(root.right);
47+
max = Math.max(max, left + right);
48+
console.log(left, right, root.val);
49+
return Math.max(left, right);
50+
}
51+
52+
dfs(root);
53+
54+
return max;
55+
};
56+
```
57+
58+
### Optimized Solution
59+
60+
The below solution has a time and space complexity of O(n). To see an approximate explanation of the approach see this video: https://www.youtube.com/watch?v=bkxqA8Rfv04
61+
62+
I made a slight tweak in the math where instead of returning a height of -1 for a null node I returned 0 and then on valid nodes I returned a height of 1 + max(left, right). You can see discussion of the solution here: https://leetcode.com/problems/diameter-of-binary-tree/discuss/101148/Intuitive-Javascript-Solution
63+
64+
```javascript
65+
const diameterOfBinaryTree = function (root) {
66+
let max = 0;
67+
68+
function dfs(root) {
69+
if (!root) {
70+
return 0;
71+
}
72+
const left = dfs(root.left);
73+
const right = dfs(root.right);
74+
max = Math.max(max, left + right);
75+
return 1 + Math.max(left, right);
76+
}
77+
78+
dfs(root);
79+
80+
return max;
81+
};
82+
```

0 commit comments

Comments
 (0)