Binary Tree Diameter | Data Structures Interview | Skill-Lync Resources
Medium Data Structures Trees & Binary Trees

How do you find the diameter of a binary tree?

Answer

Diameter is the longest path between any two nodes (number of edges). For each node, the longest path through it is left_height + right_height. Use DFS to compute height while tracking maximum diameter seen. At each node, calculate current diameter as left_height + right_height, update global maximum, return 1 + max(left_height, right_height). Time complexity is O(n), visiting each node once.

Master These Concepts with IIT Certification
IIT Certified

Master These Concepts with IIT Certification

175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.

Relevant for Roles

Software Engineer Backend Developer Algorithm Developer