Count levels in a Binary Tree consisting of nodes valued 1 grouped together
Given a Binary Tree consisting of 0s and 1s only, the task is to print the count of levels in the Binary Tree in which all the 1s are placed consecutively in a single group.
Examples:
Input: 0
/ \
1 0
/ \ / \
1 0 1 0
Output: 2
Explanation: In Levels 1 and 2, all the nodes with value 1 are placed consecutively.
Input: 0
/ \
1 0
/ \ \
1 1 0
/ \ \ / \
1 1 1 0 0
Output: 4
Explanation: In all the levels, nodes with value 1 are placed consecutively.
Approach: Follow the steps below to solve the problem:
- Perform Level Order Traversal using Queue.
- Traverse each level of the Binary Tree and consider following three variables:
- flag1: Sets to 1 after first occurrence of node with value 1.
- flag0: Sets to 1 after first occurrence of node with value 0 after occurrence of any node with value 1.
- flag2: Sets after first occurrence of node with value 1 after both flag0 and flag1 are set to 1.
- After traversing each level, check if flag1 is set to 1 and flag2 is 0. If found to be true, include that level in the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
struct node* left;
int data;
struct node* right;
};
int countLevels(node* root)
{
if (root == NULL)
return 0;
int Count = 0;
queue<node*> q;
node* curr;
q.push(root);
while (!q.empty()) {
int n = q.size();
int flag0 = 0, flag1 = 0, flag2 = 0;
while (n--) {
curr = q.front();
q.pop();
if (curr) {
if (curr->left)
q.push(curr->left);
if (curr->right)
q.push(curr->right);
if (curr->data == 1) {
if (!flag1)
flag1 = 1;
if (flag1 && flag0)
flag2 = 1;
}
if (curr->data == 0 && flag1)
flag0 = 1;
}
}
if (flag1 && !flag2)
Count++;
}
return Count;
}
node* newNode( int data)
{
node* temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
node* root = newNode(0);
root->left = newNode(0);
root->right = newNode(1);
root->left->left = newNode(0);
root->left->right = newNode(1);
root->right->left = newNode(1);
root->right->right = newNode(0);
cout << countLevels(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class node
{
node left;
int data;
node right;
};
static int countLevels(node root)
{
if (root == null )
return 0 ;
int Count = 0 ;
Queue<node> q = new LinkedList<>();
node curr;
q.add(root);
while (!q.isEmpty())
{
int n = q.size();
int flag0 = 0 , flag1 = 0 , flag2 = 0 ;
while (n-- > 0 )
{
curr = q.peek();
q.remove();
if (curr != null )
{
if (curr.left != null )
q.add(curr.left);
if (curr.right != null )
q.add(curr.right);
if (curr.data == 1 )
{
if (flag1 == 0 )
flag1 = 1 ;
if (flag1 > 0 && flag0 > 0 )
flag2 = 1 ;
}
if (curr.data == 0 && flag1 > 0 )
flag0 = 1 ;
}
}
if (flag1 > 0 && flag2 == 0 )
Count++;
}
return Count;
}
static node newNode( int data)
{
node temp = new node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void main(String[] args)
{
node root = newNode( 0 );
root.left = newNode( 0 );
root.right = newNode( 1 );
root.left.left = newNode( 0 );
root.left.right = newNode( 1 );
root.right.left = newNode( 1 );
root.right.right = newNode( 0 );
System.out.print(countLevels(root));
}
}
|
Python3
from collections import deque
class node:
def __init__( self ):
self .left = None
self .data = 0
self .right = None
def countLevels(root):
if (root = = None ):
return 0
Count = 0
q = deque()
curr = node()
q.append(root)
while q:
n = len (q)
flag0 = 0
flag1 = 0
flag2 = 0
while (n):
curr = q[ 0 ]
q.popleft()
if (curr):
if (curr.left):
q.append(curr.left)
if (curr.right):
q.append(curr.right)
if (curr.data = = 1 ):
if ( not flag1):
flag1 = 1
if (flag1 and flag0):
flag2 = 1
if (curr.data = = 0 and flag1):
flag0 = 1
n - = 1
if (flag1 and not flag2):
Count + = 1
return Count
def newNode(data):
temp = node()
temp.data = data
temp.left = None
temp.right = None
return temp
if __name__ = = "__main__" :
root = newNode( 0 )
root.left = newNode( 0 )
root.right = newNode( 1 )
root.left.left = newNode( 0 )
root.left.right = newNode( 1 )
root.right.left = newNode( 1 )
root.right.right = newNode( 0 )
print (countLevels(root))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class node
{
public node left;
public int data;
public node right;
};
static int countLevels(node root)
{
if (root == null )
return 0;
int Count = 0;
Queue<node> q = new Queue<node>();
node curr;
q.Enqueue(root);
while (q.Count != 0)
{
int n = q.Count;
int flag0 = 0, flag1 = 0, flag2 = 0;
while (n-- >0)
{
curr = q.Peek();
q.Dequeue();
if (curr != null )
{
if (curr.left != null )
q.Enqueue(curr.left);
if (curr.right != null )
q.Enqueue(curr.right);
if (curr.data == 1)
{
if (flag1 == 0)
flag1 = 1;
if (flag1 > 0 && flag0 > 0)
flag2 = 1;
}
if (curr.data == 0 && flag1 > 0)
flag0 = 1;
}
}
if (flag1 > 0 && flag2 == 0)
Count++;
}
return Count;
}
static node newNode( int data)
{
node temp = new node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void Main(String[] args)
{
node root = newNode(0);
root.left = newNode(0);
root.right = newNode(1);
root.left.left = newNode(0);
root.left.right = newNode(1);
root.right.left = newNode(1);
root.right.right = newNode(0);
Console.Write(countLevels(root));
}
}
|
Javascript
<script>
class node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let temp = new node(data);
return temp;
}
function countLevels(root)
{
if (root == null )
return 0;
let Count = 0;
let q = [];
let curr;
q.push(root);
while (q.length > 0)
{
let n = q.length;
let flag0 = 0, flag1 = 0, flag2 = 0;
while (n-- >0)
{
curr = q[0];
q.shift();
if (curr != null )
{
if (curr.left != null )
q.push(curr.left);
if (curr.right != null )
q.push(curr.right);
if (curr.data == 1)
{
if (flag1 == 0)
flag1 = 1;
if (flag1 > 0 && flag0 > 0)
flag2 = 1;
}
if (curr.data == 0 && flag1 > 0)
flag0 = 1;
}
}
if (flag1 > 0 && flag2 == 0)
Count++;
}
return Count;
}
let root = newNode(0);
root.left = newNode(0);
root.right = newNode(1);
root.left.left = newNode(0);
root.left.right = newNode(1);
root.right.left = newNode(1);
root.right.right = newNode(0);
document.write(countLevels(root));
</script>
|
Time Complexity: O(N)
Auxiliary Space : O(N)
Last Updated :
18 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...