848. Diameter of Binary Tree

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, return its diameter — the length (number of edges) of the longest path between any two nodes. The path may or may not pass through the root. The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Integer — the diameter in edges.

Examples

Example 1
Input: [1,2,3,4,5,null,6]
Output: 4
Explanation: Longest path spans 4 edges, e.g. 4-2-1-3-6.
Example 2
Input: [1,2]
Output: 1
Explanation: One edge.
Example 3
Input: [1]
Output: 0
Explanation: Single node, no edges.

Constraints

Asked by

MetaAmazonOracleBloombergGoogleFlipkart
Solve this problem in the editor →