- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count levels in a Binary Tree consisting of nodes valued 1 grouped together

A binary tree is a tree where each node has a maximum of two children. We are given a binary tree that consists of only 0 and 1 as the node values. We have to find the number of levels of the binary tree that consists of at least one 1 and all the ones of that level must be present consecutively.

## Example

Let's understand with the help of an example −

### Input

0 / \ 1 0 / \ / \ 1 1 0 0

### Output

2

### Explanation

For both the second and third level we have all the ones are present at the consecutive places.

**For the level 1 we have**− 0 (no one present)**For the level 2 we have**− 1 0(only 1 one is present)**For the level 3 we have**− 1 1 0 0 (both the ones are present together)

## Approach

In this approach, we are going to implement the BFS or the level order traverse to get all the nodes values present at the same level and then we will find which level have the consecutive ones present.

We will use the queue data structure to implement the BFS approach which will work on the principle of first in first out.

Also, we will use the two while nested while loop to get the next level and traverse over the current level where in each level we will maintain three variables that will store the data of the ones. Let us see the code implementation.

### Example

#include <bits/stdc++.h> using namespace std; // defining class to create structure of the binary tree node class Tree{ public: int data; // varialbe to store the data Tree* left; // pointer to store the left child address Tree* right; // pointer to store the right child address // constructor to initialize a new node Tree(int val){ data = val; left = nullptr; // making left and right pointer null-pointers right = nullptr; } }; // function to get the number of levels int countLevels(Tree* root){ queue<Tree*> q; // queue to store the levelwise values of the tree nodes // defining a base condition if(root == nullptr){ return 0; } // defining required variables int ans = 0; // varaible to store the number of levels int k; // variable to store the number of nodes present in the current level int var1; // first variable indicating the whether any 1 is present in the current level or not int var2; // second variable indicating whether previous node was 1 or not int var3; // third variable to indicate any non-consequtive one is present q.push(root); // traversing over the queue while(q.size()){ int k = q.size();// variable to collect current size of queue var1 = false; // marking all the variables to false var2 = false; var3 = false; while(k--){ Tree* cur = q.front(); q.pop(); // removing first element of queue if(cur->data == 1){ // if the current node value is 1 if(var1 == false){ // var1 false means no one is trigged yet making both var1 and var2 true to represent 1 is present in level and previous node was also one var1 = true; var2 = true; } else if(var2 == false){ // if var1 is true then we check this if var2 is false means there is already non-consecutive ones' present var3 = true; } } else{ // breaking streak of ones by marking var2 as false var2 = false; } // if left child of the current node is not null add it to queue if(cur->left){ q.push(cur->left); } // if right child of the current node is not null add it to the queue if(cur->right){ q.push(cur->right); } } // if all the ones are present consecutively if(var1 && !var3){ ans++; } } return ans;// returning the final answer } int main(){ // defining the tree Tree* root = new Tree(0); root->left = new Tree(0); root->right = new Tree(1); root->left->left = new Tree(0); root->left->right = new Tree(1); root->right->left = new Tree(1); root->right->right = new Tree(0); // calling the function to get the count cout << "The number of levels which contains consecutive ones are: " << countLevels(root); return 0; }

### Output

The number of levels which contains consecutive ones are: 2

### Time and Space Complexity

The time complexity of the above code is O(N) which is linear time complexity, where N is the total number of nodes in the given tree.

The space complexity of the above code is O(N), as we are using extra space in the form of the queue to store the elements in the queue.

## Conclusion

In this tutorial, we have implemented a program to find the number of levels of the given binary tree that only contains the binary numbers as the node value are the consists of the ones and all of them present consecutively. We have implemented the bfs approach to find all the nodes at the same level in the O(N) time and O(N) space complexity.