Given a Binary Tree, the task is to count the nodes whose immediate children are co-prime.
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:
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.
- Count of nodes in a Binary tree with immediate children as its factors
- Count of Nodes whose both immediate children are its prime factors
- Count nodes with two children at level L in a Binary Tree
- Count all pairs of divisors of a number N whose sum is coprime with N
- Print all Coprime path of a Binary Tree
- Check whether every node of binary tree has a value K on itself or its any immediate neighbours
- Count of nodes in a Binary Tree whose child is its prime factors
- Maximum parent children sum in Binary tree
- Iterative approach to check for children sum property in a Binary Tree
- Count of integers up to N which are non divisors and non coprime with N
- Print the nodes of the Binary Tree whose height is a Prime number
- Count nodes in the given tree whose weight is a fibonacci number
- Count the nodes in the given tree whose weight is prime
- Count of all prime weight nodes between given nodes in the given Tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Finding a Non Transitive Coprime Triplet in a Range
- Largest number less than or equal to N/2 which is coprime to N
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Partition first N natural number into two sets such that their sum is not coprime
- Largest Coprime Set Between two integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.