Katkat

Compute binary tree diameter

MetaSoftware Engineer · Technical Screen

Last updated

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Compute binary tree diameter states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given the root of a binary tree, compute the tree's diameter defined as the maximum number of edges on any path between two nodes. Return the diameter, explain whether paths must pass through the root, and provide a solution with time and space complexity analysis. Handle empty and single-node trees.