1200. Diameter of Binary Tree (DP)

MediumDynamic ProgrammingTree DP

Given a binary tree (level-order with null for missing nodes), return its diameter — the number of edges on the longest path between any two nodes. The input is JSON {tree}.

Input: JSON {tree}.

Output: Integer — the diameter in edges.

Examples

Example 1
Input: {"tree":[1,2,3,4,5]}
Output: 3
Explanation: Path 4-2-1-3 (or 5-2-1-3).
Example 2
Input: {"tree":[1,2]}
Output: 1
Explanation: One edge.
Example 3
Input: {"tree":[1]}
Output: 0
Explanation: Single node.

Constraints

Asked by

MetaAmazonOracleBloombergGoogleFlipkart
Solve this problem in the editor →