|
| 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