Diameter of Binary Tree Problem

Diameter of Binary Tree Problem — ExecCode Easy DSA Practice

Solve the Diameter of Binary Tree problem on ExecCode. Free online easy DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

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.

Examples

Input root = [1, 2, 3, 4, 5, null, null, 6, 7, null, 8, 9, null, null, null, 10, 11]; Output 6. Input root = [1]; Output 0. 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

Practice Diameter of Binary Tree free on ExecCode. Browse DSA problems, topic map, and placement guides.