Count of nodes in a Binary tree with immediate children as its factors
Last Updated :
05 Mar, 2023
Given a Binary Tree, the task is to print the count of nodes whose immediate children are its factors.
Examples:
Input:
1
/ \
15 20
/ \ / \
3 5 4 2
\ /
2 3
Output: 2
Explanation:
Children of 15 (3, 5)
are factors of 15
Children of 20 (4, 2)
are factors of 20
Input:
7
/ \
210 14
/ \ \
70 14 30
/ \ / \
2 5 10 15
/
23
Output:3
Explanation:
Children of 210 (70, 14)
are factors of 210
Children of 70 (2, 5)
are factors of 70
Children of 30 (10, 15)
are factors of 30
Approach: In order to solve this problem, we need to traverse the given Binary Tree in Level Order fashion and for every node with both children, check if both the children have values which are factors of the value of the current node. If true, then count such nodes and print it 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 areChilrenFactors(
struct Node* parent,
struct Node* a,
struct Node* b)
{
if (parent->key % a->key == 0
&& parent->key % b->key == 0)
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 (areChilrenFactors(
temp, 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);
}
int main()
{
Node* root = newNode(10);
root->left = newNode(40);
root->right = newNode(36);
root->right->left = newNode(18);
root->right->right = newNode(12);
root->right->left->left = newNode(2);
root->right->left->right = newNode(6);
root->right->right->left = newNode(3);
root->right->right->right = newNode(4);
root->right->right->right->left = newNode(7);
cout << getCount(root) << endl;
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 areChilrenFactors(
Node parent,
Node a,
Node b)
{
if (parent.key % a.key == 0
&& parent.key % b.key == 0 )
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 (areChilrenFactors(
temp, 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);
}
public static void main(String[] args)
{
Node root = newNode( 10 );
root.left = newNode( 40 );
root.right = newNode( 36 );
root.right.left = newNode( 18 );
root.right.right = newNode( 12 );
root.right.left.left = newNode( 2 );
root.right.left.right = newNode( 6 );
root.right.right.left = newNode( 3 );
root.right.right.right = newNode( 4 );
root.right.right.right.left = newNode( 7 );
System.out.print(getCount(root) + "\n" );
}
}
|
Python3
from collections import deque as queue
class Node:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def areChildrenFactors(parent, a, b):
if (parent.data % a.data = = 0 and
parent.data % b.data = = 0 ):
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 (areChildrenFactors(temp, 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))
if __name__ = = '__main__' :
root = Node( 10 )
root.left = Node( 40 )
root.right = Node( 36 )
root.right.left = Node( 18 )
root.right.right = Node( 12 )
root.right.left.left = Node( 2 )
root.right.left.right = Node( 6 )
root.right.right.left = Node( 3 )
root.right.right.right = Node( 4 )
root.right.right.right.left = Node( 7 )
print (getCount(root))
|
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 areChilrenFactors(
Node parent,
Node a,
Node b)
{
if (parent.key % a.key == 0
&& parent.key % b.key == 0)
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 (areChilrenFactors(
temp, 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);
}
public static void Main(String[] args)
{
Node root = newNode(10);
root.left = newNode(40);
root.right = newNode(36);
root.right.left = newNode(18);
root.right.right = newNode(12);
root.right.left.left = newNode(2);
root.right.left.right = newNode(6);
root.right.right.left = newNode(3);
root.right.right.right = newNode(4);
root.right.right.right.left = newNode(7);
Console.Write(getCount(root) + "\n" );
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .key = key;
this .left = this .right = null ;
}
}
function areChilrenFactors(parent, a, b)
{
if (parent.key % a.key == 0 &&
parent.key % b.key == 0)
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 (areChilrenFactors(
temp, 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);
}
let root = new Node(10);
root.left = new Node(40);
root.right = new Node(36);
root.right.left = new Node(18);
root.right.right = new Node(12);
root.right.left.left = new Node(2);
root.right.left.right = new Node(6);
root.right.right.left = new Node(3);
root.right.right.right = new Node(4);
root.right.right.right.left = new Node(7);
document.write(getCount(root) + "<br>" );
</script>
|
Time Complexity: O(N) where N is the number of nodes in the binary tree.
Auxiliary Space: O(N), as Level order uses N space for the call stack, where N is the number of nodes in the binary tree.
Efficient Method 2 (Using only one traversal without extra space):
we simply traverse the binary tree in preorder fashion and if a node has left and right child then we check that these childs are the factors of root node or not. If yes we increment the count variable.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->data = key;
temp->left = temp->right = NULL;
return temp;
}
bool areChilrenFactors(Node* parent, Node* a, Node* b)
{
if (parent->data % a->data == 0 && parent->data % b->data == 0)
return true ;
else
return false ;
}
void getCount(Node* root, int & count)
{
if (root == NULL)
return ;
if (root->left != NULL && root->right != NULL) {
if (areChilrenFactors(root, root->left, root->right)) {
count++;
}
}
getCount(root->left, count);
getCount(root->right, count);
}
int main()
{
Node* root = newNode(10);
root->left = newNode(40);
root->right = newNode(36);
root->right->left = newNode(18);
root->right->right = newNode(12);
root->right->left->left = newNode(2);
root->right->left->right = newNode(6);
root->right->right->left = newNode(3);
root->right->right->right = newNode(4);
root->right->right->right->left = newNode(7);
int count = 0;
getCount(root, count);
cout << count << endl;
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node left, right;
Node( int key) {
data = key;
left = right = null ;
}
}
class Main {
static boolean areChildrenFactors(Node parent, Node a, Node b) {
if (parent.data % a.data == 0 && parent.data % b.data == 0 )
return true ;
else
return false ;
}
static void getCount(Node root, int [] count) {
if (root == null )
return ;
if (root.left != null && root.right != null ) {
if (areChildrenFactors(root, root.left, root.right)) {
count[ 0 ]++;
}
}
getCount(root.left, count);
getCount(root.right, count);
}
public static void main(String[] args) {
Node root = new Node( 10 );
root.left = new Node( 40 );
root.right = new Node( 36 );
root.right.left = new Node( 18 );
root.right.right = new Node( 12 );
root.right.left.left = new Node( 2 );
root.right.left.right = new Node( 6 );
root.right.right.left = new Node( 3 );
root.right.right.right = new Node( 4 );
root.right.right.right.left = new Node( 7 );
int [] count = { 0 };
getCount(root, count);
System.out.println(count[ 0 ]);
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def areChildrenFactors(parent, a, b):
if parent.data % a.data = = 0 and parent.data % b.data = = 0 :
return True
else :
return False
def getCount(root, count):
if root is None :
return
if root.left is not None and root.right is not None :
if areChildrenFactors(root, root.left, root.right):
count[ 0 ] + = 1
getCount(root.left, count)
getCount(root.right, count)
if __name__ = = '__main__' :
root = Node( 10 )
root.left = Node( 40 )
root.right = Node( 36 )
root.right.left = Node( 18 )
root.right.right = Node( 12 )
root.right.left.left = Node( 2 )
root.right.left.right = Node( 6 )
root.right.right.left = Node( 3 )
root.right.right.right = Node( 4 )
root.right.right.right.left = Node( 7 )
count = [ 0 ]
getCount(root, count)
print (count[ 0 ])
|
C#
using System;
class Node
{
public int data;
public Node left, right;
public Node( int key)
{
data = key;
left = right = null ;
}
}
class MainClass
{
static bool AreChildrenFactors(Node parent, Node a, Node b)
{
if (parent.data % a.data == 0 && parent.data % b.data == 0)
return true ;
else
return false ;
}
static void GetCount(Node root, int [] count)
{
if (root == null )
return ;
if (root.left != null && root.right != null )
{
if (AreChildrenFactors(root, root.left, root.right))
{
count[0]++;
}
}
GetCount(root.left, count);
GetCount(root.right, count);
}
public static void Main()
{
Node root = new Node(10);
root.left = new Node(40);
root.right = new Node(36);
root.right.left = new Node(18);
root.right.right = new Node(12);
root.right.left.left = new Node(2);
root.right.left.right = new Node(6);
root.right.right.left = new Node(3);
root.right.right.right = new Node(4);
root.right.right.right.left = new Node(7);
int [] count = {0};
GetCount(root, count);
Console.WriteLine(count[0]);
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
function newNode(key){
return new Node(key);
}
function areChilrenFactors(parent, a, b){
if (parent.data % a.data == 0 && parent.data % b.data == 0)
return true ;
else
return false ;
}
let count = 0;
function getCount(root)
{
if (root == null )
return ;
if (root.left != null && root.right != null ) {
if (areChilrenFactors(root, root.left, root.right)) {
count++;
}
}
getCount(root.left);
getCount(root.right);
}
let root = newNode(10);
root.left = newNode(40);
root.right = newNode(36);
root.right.left = newNode(18);
root.right.right = newNode(12);
root.right.left.left = newNode(2);
root.right.left.right = newNode(6);
root.right.right.left = newNode(3);
root.right.right.right = newNode(4);
root.right.right.right.left = newNode(7);
getCount(root);
console.log(count);
|
Time Complexity: O(N) where N is the number of nodes in given binary tree.
Auxiliary Space: O(N) due to recursion call stack.
Share your thoughts in the comments
Please Login to comment...