Here is the interview question prompt, presented for reference.
Let's find out the length of the diameter of the tree!
This is a simple binary tree question that is frequently asked in interviews. So let's see how we can solve this problem.
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.
Note: The length of a path between two nodes is represented by the number of edges between them.
![Problem Statement](https://storage.googleapis.com/algodailyrandomassets/curriculum/trees/diameter-of-binary-tree/Problem.png)
Given the Binary Tree above as the problem where,
Input: root = [1, 2, 3, 4, 5]
Output: length = 3
We get to solution three by taking the paths a, b, and c
or d, b, and c
, as shown in the image below:
![Problem Statement2](https://storage.googleapis.com/algodailyrandomassets/curriculum/trees/diameter-of-binary-tree/Problem(2).png)
[1, 10^4) ]
.-100 <= Node.val <= 100
You can see the full challenge with visuals at this link.
Challenges • Asked over 2 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Diameter of Binary Tree (Main Thread).