Skip to content

Practice Diameter of Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#543
LeetCode Problem

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.

Source: Diameter of Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [1,2,3,4,5]
Output
3

# Example 2

Input
root = [1,2]
Output
1

Constraints

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

Related LeetCode Problems

Practice more problems similar to Diameter of Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.