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++
// C++ program for Counting nodes // whose immediate children // are co-prime #include <bits/stdc++.h> using namespace std; // Structure of node struct Node { int key; struct Node *left, *right; }; // Utility function to // create a new node Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } // Function to check and print if // two nodes are co-prime or not bool coprime(struct Node* a, struct Node* b) { if (__gcd(a->key, b->key) == 1) return true; else return false; } // Function to get the count of // Nodes whose immediate children // are co-prime in a binary tree unsigned int getCount(struct Node* node) { // Base Case if (!node) return 0; queue<Node*> q; // Do level order traversal // starting from root 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; } // Function to find total // number of nodes // In a given binary tree int findSize(struct Node* node) { // Base condition if (node == NULL) return 0; return 1 + findSize(node->left) + findSize(node->right); } // Function to create Tree // and find the count of nodes // whose immediate children // are co-prime void findCount() { /* 10 / \ 48 12 / \ 18 35 / \ / \ 21 29 43 16 / 7 */ // Create Binary Tree 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); // Print all nodes // with Co-Prime children cout << getCount(root) << endl; } // Driver Code int main() { // Function Call findCount(); return 0; } |
chevron_right
filter_none
Java
// Java program for Counting nodes // whose immediate children // are co-prime import java.util.*; class GFG{ // Structure of node static class Node { int key; Node left, right; }; // Utility function to // create a new node static Node newNode(int key) { Node temp = new Node(); temp.key = key; temp.left = temp.right = null; return (temp); } // Function to check and print if // two nodes are co-prime or not static boolean coprime(Node a, Node b) { if (__gcd(a.key, b.key) == 1) return true; else return false; } // Function to get the count of // Nodes whose immediate children // are co-prime in a binary tree static int getCount(Node node) { // Base Case if (node == null) return 0; Queue<Node> q = new LinkedList<Node>(); // Do level order traversal // starting from root 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; } // Function to find total // number of nodes // In a given binary tree static int findSize(Node node) { // Base condition if (node == null) return 0; return 1 + findSize(node.left) + findSize(node.right); } // Function to create Tree // and find the count of nodes // whose immediate children // are co-prime static void findCount() { /* 10 / \ 48 12 / \ 18 35 / \ / \ 21 29 43 16 / 7 */ // Create Binary Tree 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); // Print all nodes // with Co-Prime children System.out.print(getCount(root) +"\n"); } static int __gcd(int a, int b) { return b == 0? a:__gcd(b, a % b); } // Driver Code public static void main(String[] args) { // Function Call findCount(); } } // This code is contributed by sapnasingh4991 |
chevron_right
filter_none
C#
// C# program for Counting nodes // whose immediate children // are co-prime using System; using System.Collections.Generic; class GFG{ // Structure of node class Node { public int key; public Node left, right; }; // Utility function to // create a new node static Node newNode(int key) { Node temp = new Node(); temp.key = key; temp.left = temp.right = null; return (temp); } // Function to check and print if // two nodes are co-prime or not static bool coprime(Node a, Node b) { if (__gcd(a.key, b.key) == 1) return true; else return false; } // Function to get the count of // Nodes whose immediate children // are co-prime in a binary tree static int getCount(Node node) { // Base Case if (node == null) return 0; List<Node> q = new List<Node>(); // Do level order traversal // starting from root 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; } // Function to find total // number of nodes // In a given binary tree static int findSize(Node node) { // Base condition if (node == null) return 0; return 1 + findSize(node.left) + findSize(node.right); } // Function to create Tree // and find the count of nodes // whose immediate children // are co-prime static void findCount() { /* 10 / \ 48 12 / \ 18 35 / \ / \ 21 29 43 16 / 7 */ // Create Binary Tree 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); // Print all nodes // with Co-Prime children Console.Write(getCount(root) +"\n"); } static int __gcd(int a, int b) { return b == 0? a:__gcd(b, a % b); } // Driver Code public static void Main(String[] args) { // Function Call findCount(); } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output:
3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of nodes in a Binary tree with immediate children as its factors
- Count nodes with two children at level L in a Binary Tree
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Count Non-Leaf nodes in a Binary Tree
- Print all Coprime path of a Binary Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Count the number of visible nodes in Binary Tree
- Program to count leaf nodes in a binary tree
- Iterative program to count leaf nodes in a Binary Tree
- Count half nodes in a Binary tree (Iterative and Recursive)
- Count full nodes in a Binary tree (Iterative and Recursive)
- Count of nodes in a Binary Tree whose child is its prime factors
- Count of Nodes whose both immediate children are its prime factors
- Maximum parent children sum in Binary tree
- Check for Children Sum Property in a Binary Tree
- Iterative approach to check for children sum property in a Binary Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Count of all prime weight nodes between given nodes in the given Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Sum of all nodes in a binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

