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
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count of nodes in a Binary Tree whose child is its prime factors
- Node having maximum sum of immediate children and itself in n-ary tree
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Count numbers from range whose prime factors are only 2 and 3
- 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
- Check whether every node of binary tree has a value K on itself or its any immediate neighbours
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Count nodes with two children at level L in a Binary Tree
- Product of divisors of a number from a given list of its prime factors
- Super Ugly Number (Number whose prime factors are in given set)
- Sum of element whose prime factors are present in array
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Number of full binary trees such that each node is product of its children
- Count the nodes in the given tree whose weight is prime
- Count of all prime weight nodes between given nodes in the given 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 email@example.com. 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