LeetCode 543. Diameter of Binary Tree
The diameter of a binary tree is defined as the length of the longest path between any two nodes within the tree. The path does not necessarily have to pass through the root.
The length of a path between two nodes in a binary tree is the number of edges between the nodes. Note that the path cannot include the same node twice.
Given the root of a binary tree root, return the diameter of the tree.
Examples
Example 1
Input: root = [1,2,3]
Output: 2
The longest path is [2,1,3] with length 2.
Example 2
Input: root = [1,2]
Output: 1
The longest path is [2,1] with length 1.
Constraints
1 <= number of nodes in the tree <= 100-100 <= Node.val <= 100
How the tests work: trees are written as level-order arrays —
[1,null,2]is a root1with a right child2. Your function receives realTreeNodeobjects.