Structurally Unique Binary Trees of Dragon Balls

Goku has ‘N’ Dragon Balls, where each Dragon Ball is unique. The ith Dragon Ball has ‘i’ stars on it, meaning the first Dragon Ball has 1 star, the second has 2 stars, and so on.

Gohan needs to arrange these ‘N’ Dragon Balls in a binary tree format, ensuring the following conditions are met:

  • The left subtree of any Dragon Ball ‘D’ should only have Dragon Balls with fewer stars than ‘D’.
  • The right subtree of any Dragon Ball ‘D’ should only have Dragon Balls with more stars than ‘D’.

Objective:

Determine how many structurally unique binary trees can be constructed following the conditions provided. Return the count of these trees modulo 109 + 7.

Input:
First line contains an integer 'T', the number of test cases.
Then, for each test case, a single integer ‘N’ is given, denoting the number of Dragon Balls.
Output:
Output the number of structurally unique binary trees for each test case on a new line.
Example:
Input:
2
2
3
Output:
2
5

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • Time Limit: 1 second
Note:

You do not need to print anything; it has been managed for you. Implement the function provided.

AnswerBot
4mo

Count the number of structurally unique binary trees that can be constructed with given Dragon Balls.

  • Use dynamic programming to solve this problem efficiently.

  • The number of structurally unique binary ...read more

Help your peers!
Select
Add answer anonymously...

Atlassian Software Developer interview questions & answers

A Software Developer was asked Q. Implement the snake game.
A Software Developer was asked Q. Find total size
A Software Developer was asked Q. How would you find the top k collections?

Popular interview questions of Software Developer

A Software Developer was asked Q1. Implement the snake game.
A Software Developer was asked Q2. Find total size
A Software Developer was asked Q3. How would you find the top k collections?
Atlassian 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