Binary Tree Diameter Problem Statement

You are given a Binary Tree, and you need to determine the length of the diameter of the tree.

The diameter of a binary tree is the length of the longest path between any two end nodes in the tree.

Input:

The first line contains an integer 'T', representing the number of test cases. Each test case consists of a single line listing the tree nodes in level order, with nodes separated by spaces. Use -1 to denote a null node.

Output:

For each test case, output a single integer representing the diameter of the binary tree. Each result should be printed on a new line.
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
6
Explanation:

The provided input represents a binary tree. The nodes contributing to the diameter are highlighted, and the length of the path between them is 6.

Constraints:

  • 1 ≤ T ≤ 10
  • 0 ≤ N ≤ 105
  • 1 ≤ data ≤ 104

Where ‘N’ is the total number of nodes in the binary tree, and 'data' represents the node values.

Note: You are not required to handle input/output operations. Implement the function to compute the result.

Be the first one to answer
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Intuit

Q. How do you debug C++ code?
Q. What is polymorphism?
Q. Rearrange Linked List Problem Statement Given a singly linked list in the form '...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits