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!
Select
Add answer anonymously...

Top Associate Software Engineer Interview Questions Asked at Publicis Sapient

Q. Where are the job locations of Publicis Sapient in India?
Q. What is Event loop? What is higher order components?
Q. Merge Two Sorted Arrays Problem Statement Given two sorted integer arrays ARR1 a...read more
Associate Software Engineer 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