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
2d

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!
Add answer anonymously...
Intuit Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter