Diameter Of Binary Tree is an example of tree problems. In this post we will see how we can solve in Javascript.

Problem Description

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree.

This path may or may not pass through the root.

Example: Given a binary tree

      1
     / \
    2   3
   / \
  4   5

Return 4, which is the length of the path [4,2,1,3] or [5,2,1,3].

Implementation: Please check the main.js snippet for the solution. If you have different approach in mind or have any suggestion for this implementation feel free to share in the comment below. Thanks!