Given a Binary Tree, the task is to print the count of nodes having both children and both of them are its prime factors.
Input: 1 / \ 15 20 / \ / \ 3 5 4 2 \ / 2 3 Output: 1 Explanation: Children of 15 (3, 5) are prime factors of 15 Input: 7 / \ 210 14 / \ \ 70 14 30 / \ / \ 2 5 3 5 / 23 Output: 2 Explanation: Children of 70 (2, 5) are prime factors of 70 Children of 30 (3, 5) are prime factors of 30
- Traverse the given Binary Tree and for each node, check both the children exists or not.
- If both the children exist, check if each child is a prime factor of this node or not.
- Keep the count of such nodes and print it at the end.
- In order to check if a factor is prime, we will use Sieve to precompute the prime numbers to do the checking in O(1).
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 in a Binary Tree whose immediate children are co-prime
- Count of nodes in a Binary Tree whose child is its prime factors
- Count nodes with two children at level L in a Binary Tree
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count of all prime weight nodes between given nodes in the given Tree
- Count numbers from range whose prime factors are only 2 and 3
- Count common prime factors of two numbers
- Count of prime factors of N to be added at each step to convert N to M
- Find the row whose product has maximum count of prime factors
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Check if a number exists having exactly N factors and K prime factors
- Count the nodes in the given tree whose weight is prime
- Count of Nodes which has Prime Digit sum weight in a Tree
- Maximum number of prime factors a number can have with exactly x factors
- Prime factors of a big number
- Distinct Prime Factors of an Array
- Prime factors of LCM of array elements
- Sum of Factors of a Number using Prime Factorization
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.
Improved By : Rajput-Ji