Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Diameter of Binary Tree (Main Thread)

Here is the interview question prompt, presented for reference.

Diameter of Binary Tree

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.

Prompt

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)

Constraints

  • The number of nodes in the tree is in the range [1, 10^4) ].
  • -100 <= Node.val <= 100

You can see the full challenge with visuals at this link.

Challenges • Asked over 1 year ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Aug 17, 2022:

This is the main discussion thread generated for Diameter of Binary Tree (Main Thread).