Tree DFS
LC #543Easy
Diameter of Binary Tree
Tree DFS
AmazonGoogleMetaBloombergProblem
Find the length of the longest path between any two nodes in the tree (the diameter).
treedfs
Constraints
- ›1 ≤ n ≤ 10⁴
- ›-100 ≤ Node.val ≤ 100
- ›Path does not need to pass through root
Example
Input
root = [1, 2, 3, 4, 5]Output
3Why
Longest path is [4,2,1,3] or [5,2,1,3], length = 3 edges