Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Diameter of Binary Tree

Easy
treebinary-treedfs

Return the diameter of a binary tree: the number of edges on the longest path between any two nodes (the path need not pass through the root).

Input: one line — the tree in level order. Output: the diameter.

Example 1
Input:
1 2 3 4 5
Output:
3

Path 4 → 2 → 1 → 3 (3 edges).

Example 2
Input:
1 2
Output:
1

Constraints

  • 1 ≤ nodes ≤ 10^4
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.