Find diameter of binary tree

TopicDifficultyCompanies
Binary Tree
MEDIUM
Facebook
Google
Amazon

Given a binary tree, write a program to compute the length of the diameter of the tree.

Problem Note

  • The diameter of a binary tree is defined as the length of the longest path between any two nodes in a tree. This path may or may not contain the root in it.
  • The length of the path between two nodes is represented by the number of edges between them.

Example

Input: Given a binary tree
2
/ \
4 5
/ \
6 7
Output: 3
Explanation: Return 3, which is the length(number of edges) of the path [6,4,2,5] or [5,2,4,6]

Code Editor

Practice and Learn

Best way to learn is through solving real problems. Practice this problem in this code editor.