Given a Binary Tree, the task is to count the nodes whose immediate children are co-prime.
Examples:
Input:
1
/ \
15 5
/ \ / \
11 2 4 15
\ /
2 3
Output: 2
Explanation:
Children of 15 (11, 2) are co-prime
Children of 5 (4, 15) are co-prime
Input:
7
/ \
21 14
/ \ \
77 16 3
/ \ / \
2 5 10 11
/
23
Output:3
Explanation:
Children of 21 (77, 8) are co-prime
Children of 77 (2, 5) are co-prime
Children of 3 (10, 11) are co-prime
Approach: The idea is to:
- Do level order traversal of the tree
- For each node check that its both children are not Null
- If true, then check whether greatest common divisor of both children is 1.
- If yes, then count such nodes and print at the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
struct Node *left, *right;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
bool coprime( struct Node* a,
struct Node* b)
{
if (__gcd(a->key, b->key) == 1)
return true ;
else
return false ;
}
unsigned int getCount( struct Node* node)
{
if (!node)
return 0;
queue<Node*> q;
int count = 0;
q.push(node);
while (!q.empty()) {
struct Node* temp = q.front();
q.pop();
if (temp->left && temp->right) {
if (coprime(temp->left,
temp->right))
count++;
}
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
}
return count;
}
int findSize( struct Node* node)
{
if (node == NULL)
return 0;
return 1
+ findSize(node->left)
+ findSize(node->right);
}
void findCount()
{
Node* root = newNode(10);
root->left = newNode(48);
root->right = newNode(12);
root->right->left = newNode(18);
root->right->right = newNode(35);
root->right->left->left = newNode(21);
root->right->left->right = newNode(29);
root->right->right->left = newNode(43);
root->right->right->right = newNode(16);
root->right->right->right->left = newNode(7);
cout << getCount(root) << endl;
}
int main()
{
findCount();
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int key;
Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return (temp);
}
static boolean coprime(Node a,
Node b)
{
if (__gcd(a.key, b.key) == 1 )
return true ;
else
return false ;
}
static int getCount(Node node)
{
if (node == null )
return 0 ;
Queue<Node> q = new LinkedList<Node>();
int count = 0 ;
q.add(node);
while (!q.isEmpty()) {
Node temp = q.peek();
q.remove();
if (temp.left != null && temp.right != null ) {
if (coprime(temp.left,
temp.right))
count++;
}
if (temp.left != null )
q.add(temp.left);
if (temp.right != null )
q.add(temp.right);
}
return count;
}
static int findSize(Node node)
{
if (node == null )
return 0 ;
return 1
+ findSize(node.left)
+ findSize(node.right);
}
static void findCount()
{
Node root = newNode( 10 );
root.left = newNode( 48 );
root.right = newNode( 12 );
root.right.left = newNode( 18 );
root.right.right = newNode( 35 );
root.right.left.left = newNode( 21 );
root.right.left.right = newNode( 29 );
root.right.right.left = newNode( 43 );
root.right.right.right = newNode( 16 );
root.right.right.right.left = newNode( 7 );
System.out.print(getCount(root) + "\n" );
}
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
public static void main(String[] args)
{
findCount();
}
}
|
Python3
from collections import deque as queue
from math import gcd as __gcd
class Node:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def coprime(a, b):
if (__gcd(a.data, b.data) = = 1 ):
return True
else :
return False
def getCount(node):
if ( not node):
return 0
q = queue()
count = 0
q.append(node)
while ( len (q) > 0 ):
temp = q.popleft()
if (temp.left and temp.right):
if (coprime(temp.left, temp.right)):
count + = 1
if (temp.left ! = None ):
q.append(temp.left)
if (temp.right ! = None ):
q.append(temp.right)
return count
def findSize(node):
if (node = = None ):
return 0
return ( 1 + findSize(node.left) +
findSize(node.right))
def findCount():
root = Node( 10 )
root.left = Node( 48 )
root.right = Node( 12 )
root.right.left = Node( 18 )
root.right.right = Node( 35 )
root.right.left.left = Node( 21 )
root.right.left.right = Node( 29 )
root.right.right.left = Node( 43 )
root.right.right.right = Node( 16 )
root.right.right.right.left = Node( 7 )
print (getCount(root))
if __name__ = = '__main__' :
findCount()
|
C#
using System;
using System.Collections.Generic;
class GFG{
class Node {
public int key;
public Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return (temp);
}
static bool coprime(Node a,
Node b)
{
if (__gcd(a.key, b.key) == 1)
return true ;
else
return false ;
}
static int getCount(Node node)
{
if (node == null )
return 0;
List<Node> q = new List<Node>();
int count = 0;
q.Add(node);
while (q.Count != 0) {
Node temp = q[0];
q.RemoveAt(0);
if (temp.left != null && temp.right != null ) {
if (coprime(temp.left,
temp.right))
count++;
}
if (temp.left != null )
q.Add(temp.left);
if (temp.right != null )
q.Add(temp.right);
}
return count;
}
static int findSize(Node node)
{
if (node == null )
return 0;
return 1
+ findSize(node.left)
+ findSize(node.right);
}
static void findCount()
{
Node root = newNode(10);
root.left = newNode(48);
root.right = newNode(12);
root.right.left = newNode(18);
root.right.right = newNode(35);
root.right.left.left = newNode(21);
root.right.left.right = newNode(29);
root.right.right.left = newNode(43);
root.right.right.right = newNode(16);
root.right.right.right.left = newNode(7);
Console.Write(getCount(root) + "\n" );
}
static int __gcd( int a, int b)
{
return b == 0? a:__gcd(b, a % b);
}
public static void Main(String[] args)
{
findCount();
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .key = key;
this .left = this .right = null ;
}
}
function coprime(a, b)
{
if (__gcd(a.key, b.key) == 1)
return true ;
else
return false ;
}
function getCount(node)
{
if (node == null )
return 0;
let q = [];
let count = 0;
q.push(node);
while (q.length != 0)
{
let temp = q.shift();
if (temp.left != null &&
temp.right != null )
{
if (coprime(temp.left,
temp.right))
count++;
}
if (temp.left != null )
q.push(temp.left);
if (temp.right != null )
q.push(temp.right);
}
return count;
}
function findSize(node)
{
if (node == null )
return 0;
return 1 + findSize(node.left) +
findSize(node.right);
}
function findCount()
{
let root = new Node(10);
root.left = new Node(48);
root.right = new Node(12);
root.right.left = new Node(18);
root.right.right = new Node(35);
root.right.left.left = new Node(21);
root.right.left.right = new Node(29);
root.right.right.left = new Node(43);
root.right.right.right = new Node(16);
root.right.right.right.left = new Node(7);
document.write(getCount(root) + "<br>" );
}
function __gcd(a,b)
{
return b == 0? a:__gcd(b, a % b);
}
findCount();
</script>
|
Complexity Analysis:
Time Complexity: O(N*logV) where V is the weight of a node in the tree.
In bfs, every node of the tree is processed once and hence the complexity due to the bfs is O(N) if there are total N nodes in the tree. Also, while processing every node, in order to check if the node values are co-prime or not, the inbuilt __gcd(A, B) function where A, B are the weight of the nodes is being called and this function has a complexity of O(log(min(A, B))), hence for every node there is an added complexity of O(logV). Therefore, the time complexity is O(N*logV).
Auxiliary Space : O(w) where w is the maximum width of the tree.