Tree Diameter
Write the code, see it run, pass the checks.
The problem
- medium
- Code
- Pro
Write diameter(root). A binary tree is made of nodes { value, left, right }, where left and right are child nodes or null; an empty tree is null. Return the tree's diameter: the number of edges on the longest path between any two nodes, where a path moves between parents and children and visits no node twice. It doesn't have to pass through the root. An empty tree or a single node has diameter 0. Don't change the tree. For example, the tree written in level order as [1, 2, 3, 4, 5] has diameter 3, along the path 4, 2, 1, 3. The checks include a chain of 5,000 nodes.
Your code must pass
- Measures a path through the root
- Finds a longer path that skips the root
- Returns 0 for an empty tree and a single node
- Measures a chain of 5,000 nodes