Count levels in a Binary Tree consisting of node values having set bits at different positions
Last Updated :
17 Apr, 2023
Given a Binary Tree consisting of N nodes, the task is to count the number of levels in a Binary Tree such that the set bits of all the node values at the same level is at different positions.
Examples:
Input:
5
/ \
6 9
/ \ \
1 4 7
Output: 2
Explanation:
Level 1 has only 5 (= (101)2).
Level 2 has 6 (= (0110)2) and 9 (= (1001)2). All set bits are at unique positions.
Level 3 has 1 (0001)2, 4 (0100)2 and 7(0111)2. Therefore, 0th bit of node values 5 and 7 are set.
Input:
1
/ \
2 3
/ \ \
5 4 7
Output: 1
Naive Approach: The simplest approach to solve this problem to traverse the binary tree using level order traversal and at each level of the tree store the set bits of all the nodes using Map. Traverse the map and check if the frequency of set-bit at the same position is less than or equal to 1 or not. If found to be true, then increment the count. Finally, print the count obtained.
Time Complexity: O(N)
Auxiliary Space: O(32)
Efficient Approach: The above approach can be optimized based on the following observations:
If all the set bits of two numbers A and B are at different positions
A XOR B = A OR B
Follow the steps below to solve the problem:
- Initialize a variable, say prefiX_XOR, to store the prefix XOR of all the nodes at each level.
- Initialize a variable, say prefiX_OR, to store the prefix OR of all the nodes at each level.
- Traverse the binary tree using level order traversal. At every ith level, check if prefix_XOR ^ nodes is equal to (prefix_OR | nodes) or not. If found to be true for all the nodes at current level, then increment the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val = 0;
TreeNode *left,*right;
TreeNode( int x)
{
val = x;
left = NULL;
right = NULL;
}
};
void uniqueLevels(TreeNode *root)
{
int uniqueLevels = 0;
queue<TreeNode*> que;
que.push(root);
while (que.size() > 0)
{
int length = que.size();
int prefix_XOR = 0;
int prefix_OR = 0;
bool flag = true ;
for ( int i = 0; i < length; i++){
TreeNode *temp = que.front();
que.pop();
prefix_OR |= temp->val;
prefix_XOR ^= temp->val;
if (prefix_XOR != prefix_OR)
flag = false ;
if (temp->left)
que.push(temp->left);
if (temp->right)
que.push(temp->right);
}
if (flag)
uniqueLevels += 1;
}
cout << uniqueLevels;
}
int main()
{
TreeNode *root = new TreeNode(5);
root->left = new TreeNode(6);
root->right = new TreeNode(9);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(4);
root->right->right = new TreeNode(7);
uniqueLevels(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class TreeNode
{
int val = 0 ;
TreeNode left, right;
TreeNode( int x)
{
val = x;
left = null ;
right = null ;
}
};
static void uniqueLevels(TreeNode root)
{
int uniqueLevels = 0 ;
Queue<TreeNode> que = new LinkedList<>();
que.add(root);
while (que.size() > 0 )
{
int length = que.size();
int prefix_XOR = 0 ;
int prefix_OR = 0 ;
boolean flag = true ;
for ( int i = 0 ; i < length; i++)
{
TreeNode temp = que.peek();
que.remove();
prefix_OR |= temp.val;
prefix_XOR ^= temp.val;
if (prefix_XOR != prefix_OR)
flag = false ;
if (temp.left != null )
que.add(temp.left);
if (temp.right != null )
que.add(temp.right);
}
if (flag)
uniqueLevels += 1 ;
}
System.out.print(uniqueLevels);
}
public static void main(String[] args)
{
TreeNode root = new TreeNode( 5 );
root.left = new TreeNode( 6 );
root.right = new TreeNode( 9 );
root.left.left = new TreeNode( 1 );
root.left.right = new TreeNode( 4 );
root.right.right = new TreeNode( 7 );
uniqueLevels(root);
}
}
|
Python3
class TreeNode:
def __init__( self , val = 0 , left = None , right = None ):
self .val = val
self .left = left
self .right = right
def uniqueLevels(root):
uniqueLevels = 0
que = [root]
while len (que):
length = len (que)
prefix_XOR = 0 ;
prefix_OR = 0
flag = True
while length:
temp = que.pop( 0 )
prefix_OR | = temp.val
prefix_XOR ^ = temp.val
if prefix_XOR ! = prefix_OR:
flag = False
if temp.left:
que.append(temp.left)
if temp.right:
que.append(temp.right)
length - = 1
if flag:
uniqueLevels + = 1
print (uniqueLevels)
if __name__ = = '__main__' :
root = TreeNode( 5 )
root.left = TreeNode( 6 )
root.right = TreeNode( 9 )
root.left.left = TreeNode( 1 )
root.left.right = TreeNode( 4 )
root.right.right = TreeNode( 7 )
uniqueLevels(root)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
class TreeNode
{
public int val = 0;
public TreeNode left, right;
public TreeNode( int x)
{
val = x;
left = null ;
right = null ;
}
};
static void uniqueLevels(TreeNode root)
{
int uniqueLevels = 0;
Queue<TreeNode> que = new Queue<TreeNode>();
que.Enqueue(root);
while (que.Count > 0)
{
int length = que.Count;
int prefix_XOR = 0;
int prefix_OR = 0;
bool flag = true ;
for ( int i = 0; i < length; i++)
{
TreeNode temp = que.Peek();
que.Dequeue();
prefix_OR |= temp.val;
prefix_XOR ^= temp.val;
if (prefix_XOR != prefix_OR)
flag = false ;
if (temp.left != null )
que.Enqueue(temp.left);
if (temp.right != null )
que.Enqueue(temp.right);
}
if (flag)
uniqueLevels += 1;
}
Console.Write(uniqueLevels);
}
public static void Main(String[] args)
{
TreeNode root = new TreeNode(5);
root.left = new TreeNode(6);
root.right = new TreeNode(9);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(4);
root.right.right = new TreeNode(7);
uniqueLevels(root);
}
}
|
Javascript
<script>
class TreeNode
{
constructor(x)
{
this .val = x;
this .left = null ;
this .right = null ;
}
}
function uniqueLevels(root)
{
let uniqueLevels = 0;
let que = [];
que.push(root);
while (que.length > 0)
{
let length = que.length;
let prefix_XOR = 0;
let prefix_OR = 0;
let flag = true ;
for (let i = 0; i < length; i++)
{
let temp = que[0];
que.shift();
prefix_OR |= temp.val;
prefix_XOR ^= temp.val;
if (prefix_XOR != prefix_OR)
flag = false ;
if (temp.left != null )
que.push(temp.left);
if (temp.right != null )
que.push(temp.right);
}
if (flag)
uniqueLevels += 1;
}
document.write(uniqueLevels);
}
let root = new TreeNode(5);
root.left = new TreeNode(6);
root.right = new TreeNode(9);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(4);
root.right.right = new TreeNode(7);
uniqueLevels(root);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...