Mark As Completed Discussion

Good afternoon! Here's our prompt for today.

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.

Diameter of Binary Tree

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:

Diameter of Binary Tree

Constraints

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

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's our guided, illustrated walk-through.

How do I use this guide?

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.