← 返回 meta 的题目列表Diameter of Tree at Leaf Nodes
类型:online_judge
Given a binary tree, calculate the diameter among all the leaf nodes, defined as the length of the path between the two farthest leaf nodes. The tree might have leaf nodes only on one side. Implement a function tree_diameter(root: TreeNode) -> int. The input is the root of the tree, and the output is the maximum leaf node diameter.
Example
Input:
[1, 2, 3, null, 4, null, 5]
Output:
3
Explanation: The tree structure is:
1
/ \
2 3
\ \
4 5
The path 4 -> 2 -> 1 -> 3 -> 5 has a length of 3.
Constraints
The number of nodes in the tree is in the range [1, 10^4].
Each node value is unique.
Example
Input
1
2 3
null 4 null 5