Height of a Binary Tree

You are provided with an arbitrary binary tree consisting of 'N' nodes where each node is associated with a certain value. The task is to determine the height of the tree.

Explanation:

The height of a binary tree is defined as the maximum number of edges from the root to any of its leaf nodes.

Input:

The first line consists of an integer T, representing the number of test cases.

The first line of each test case contains the values of the nodes in the binary tree in level order (use -1 to represent a NULL node).

Output:

For each test case, output the height of the binary tree as a single integer.

Example:

Input:

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Output:

4

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 3000
  • Time Limit: 1 sec

Note:

There is no need to print anything explicitly; you only need to implement the function to solve the problem.

AnswerBot
1y

The height of a binary tree is the maximum number of edges from the root to a leaf node.

  • Traverse the tree recursively and keep track of the maximum height

  • If the current node is null, return 0

  • Otherwise...read more

Help your peers!
Add answer anonymously...
Publicis Sapient Associate Software Engineer 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