Skip to content

Problem 543 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
18. [Reverse Linked List #206](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/reverse-linked-list-206.md)
19. [Majority Element #169](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/majority-element-169.md)
20. [Add Binary #67](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/add-binary-67.md)
21. Diameter of Binary Tree #543
21. [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
22. Middle of the Linked List #876
23. Maximum Depth of Binary Tree #104
24. Contains Duplicate #217
Expand Down Expand Up @@ -137,6 +137,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
- [Lowest Common Ancestor of a Binary Search Tree #235](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/lowest-common-ancestor-235.md)
- [Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
- [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)

## Algorithm Patterns

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

### Divide & Conquer

Expand Down
82 changes: 82 additions & 0 deletions easy/diameter-binary-tree-543.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Diameter of Binary Tree

Page on leetcode:https://leetcode.com/problems/diameter-of-binary-tree/

## Problem Statement

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.

### Constraints

- The number of nodes in the tree is in the range [1, 104].
- -100 <= Node.val <= 100

### Example

```
Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
```

## Solution

Since we only need to return the length we can probably use a max variable. DFS seems like a decent approach.

### Pseudocode

1. Set max variable to 0
2. create dfs helper function
3. check if root is null, if true return 0
4. recursive check on left and right, add one for each call
5. set max to the max(max, left + right)
6. return max

### Initial Attempt

```javascript
const diameterOfBinaryTree = function (root) {
let max = 0;

function dfs(root) {
if (!root) {
return 0;
}
const left = 1 + dfs(root.left);
const right = 1 + dfs(root.right);
max = Math.max(max, left + right);
console.log(left, right, root.val);
return Math.max(left, right);
}

dfs(root);

return max;
};
```

### Optimized Solution

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

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

```javascript
const diameterOfBinaryTree = function (root) {
let max = 0;

function dfs(root) {
if (!root) {
return 0;
}
const left = dfs(root.left);
const right = dfs(root.right);
max = Math.max(max, left + right);
return 1 + Math.max(left, right);
}

dfs(root);

return max;
};
```