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

Problem Description


This path may or may not pass through the root.

Example: Given a binary tree

     / \
    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!