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.

AnswerBot
4mo

The task is to find the diameter of a binary tree, which is the longest path between any two nodes in the tree.

  • Traverse the tree to find the longest path between two nodes.

  • Keep track of the maximum di...read more

Help your peers!
Select
Add answer anonymously...

Intuit Software Developer Intern interview questions & answers

A Software Developer Intern was asked 6mo agoQ. How do you debug C++ code?
A Software Developer Intern was asked Q. What is polymorphism?
A Software Developer Intern was asked Q. Number of Islands Problem Statement You are provided with a 2-dimensional matrix...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 5mo agoQ1. How do you debug C++ code?
A Software Developer Intern was asked Q2. What is polymorphism?
A Software Developer Intern was asked Q3. Number of Islands Problem Statement You are provided with a 2-dimensional matrix...read more
Intuit 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