Balanced Binary Trees Problem Statement

You are provided with an integer 'H'. Your task is to determine and print the maximum number of balanced binary trees possible with height 'H'.

A balanced binary tree is defined such that for every node, the difference between the heights of the left and right subtrees is no more than 1.

Your answer should be printed modulo 1e9+7.

Example:

Input:
H = 2
Output:
3
Explanation:

There will be a total of 3 different balanced binary trees with height 2: one with a single root and other nodes branching to one side, one with the root having a left subtree which is taller by one node, and one with the root having a right subtree which is taller by one node.

Constraints:

  • 1 <= T <= 50
  • 1 <= H <= 106
  • Time Limit: 1 sec.

Input:

The first line contains a single integer 'T' representing the number of test cases to be executed. For each test case, there is a single integer 'H' indicating the height of the tree.

Output:

For each test case, produce an integer representing the number of balanced binary trees that can be constructed with the given height. Each test case's result should be printed on a new line.

Note:

You do not need to print anything as it will be handled for you. Simply implement the function and return the result.
AnswerBot
1y

The maximum number of balanced binary trees possible with a given height is to be counted and printed.

  • A balanced binary tree is one in which the difference between the left and right subtree heights i...read more

Help your peers!
Select
Add answer anonymously...

Josh Technology Group Software Developer interview questions & answers

A Software Developer was asked 9mo agoQ. Given the head of a linked list, determine if the linked list has a cycle in it....read more
A Software Developer was asked 9mo agoQ. Given a binary tree, imagine yourself standing on the left side of it, return th...read more
A Software Developer was asked 11mo agoQ. What is an API and how does it work?

Popular interview questions of Software Developer

A Software Developer was asked 11mo agoQ1. What is an API and how does it work?
A Software Developer was asked 11mo agoQ2. What is the software development life cycle, and what steps does it involve?
A Software Developer was asked Q3. Given a value, find the next sibling on the right side of it without using globa...read more
Josh Technology Group Software Developer 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