Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Binary tree diameter and max path sum



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Diameter of Binary Tree Problem

Diameter of a Binary Tree

Problem Statement

You are given a binary tree and need to find the diameter length of the tree. The diameter is defined as the length of the longest path between any two nodes in the tree. This path can pass through or bypass the root node.

Examples

Consider the binary tree shown below:

           10
          /  \
         7    8
        / \
       4   5
    

The diameter length is 3, which corresponds to the path [4,7,10,8] or [5,7,10,8].

Another example:

           3
            \
             9
              \
               15
              /  \
             20   7
    

The diameter length here is 4, with the longest path being [20,15,9,3] or [7,15,9,3].

Constraints

  • Number of nodes in the tree is between 1 and 20,000.
  • Node values are integers ranging from -1,000,000 to 1,000,000.