Leaves at Same Level Problem Statement

Given a binary tree with 'N' nodes, determine if all the leaf nodes are situated at the same level. Return true if all the leaf nodes are at the same level, otherwise return false.

A binary tree is a hierarchical data structure where each node has at most two children. A node is considered a leaf node if both its left and right children are null.

Input:

The first line contains a single integer T representing the number of test cases. 
The first line of each test case contains elements in level order form (refer to the example below), separated by space. If a node does not have a left or right child, use -1 in its place.

Output:

For each test case, print "True" if all the leaf nodes are at the same level, otherwise print "False". Each test case's output should be on a separate line.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
True
Explanation:

In the given binary tree:

  • Level 1: Node 1 is the root.
  • Level 2: Node 2 is the left child and Node 3 is the right child of 1.
  • Level 3: Node 4 is the left child of 2; Node 5 is the left child and Node 6 is the right child of 3.
  • Level 4: Node 7 is the right child of 4. Nodes 5 and 6 have no children.
  • All leaf nodes (5, 6, 7) are at the same level hence the output is True.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 3000

Time Limit: 1 sec

AnswerBot
4mo

Check if all leaf nodes in a binary tree are at the same level.

  • Traverse the binary tree and keep track of the level of each leaf node.

  • Compare the levels of all leaf nodes at the end to determine if th...read more

Help your peers!
Select
Add answer anonymously...

Oyo Rooms Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Leaves at Same Level Problem Statement Given a binary tree with 'N' nodes, deter...read more
A Software Developer Intern was asked Q. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
A Software Developer Intern was asked Q. Given the employee and department tables, how would you find the number of emplo...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Leaves at Same Level Problem Statement Given a binary tree with 'N' nodes, deter...read more
A Software Developer Intern was asked Q2. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
A Software Developer Intern was asked Q3. Given the employee and department tables, how would you find the number of emplo...read more
Oyo Rooms Software Developer Intern 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