Problem Statement: Check Whether Binary Tree Is Complete

You are provided with a binary tree. Your task is to determine if the given binary tree is a Complete Binary Tree.

A Complete Binary Tree is defined as a binary tree in which every level, except possibly the last, is entirely filled, and all nodes in the final level are positioned to the left.

Input:

The input begins with an integer T, indicating the number of test cases. For each test case, a sequence of node values is given in level order traversal, where the values are separated by spaces. If a node is null, it is represented by -1.

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

Output:

For each test case, output 1 if the Binary Tree is complete, and 0 if it is not. Continuous lines indicate individual test case outputs.

Example:

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

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 3000, where N is the total number of nodes in the binary tree.
  • 1 ≤ data ≤ 105 and data ≠ -1
Note:

No need to print anything for solving the problem; the implementation should return the results directly.

AnswerBot
1y

The task is to check whether a given binary tree is a complete binary tree or not.

  • A complete binary tree is a binary tree where every level, except possibly the last, is completely filled.

  • All nodes in...read more

Help your peers!
Add answer anonymously...
Jio Software Developer 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