Input: 1 / \ 15 20 / \ / \ 3 5 4 2 \ / 2 3 Output: 3 Explanation: Children of 15 (3, 5) are prime factors of 15 Child of 20 (2) is prime factors of 20 Child of 4 (2) is prime factors of 4 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 if children exist or not.
- If 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 of Eratosthenes 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.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count of Nodes whose both immediate children are its prime factors
- Count of nodes in a Binary tree with immediate children as its factors
- Find number of factors of N when location of its two factors whose product is N is given
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Sum of all the child nodes with even parent values in a Binary Tree
- Print the nodes having exactly one child in a Binary tree
- Sum of all the child nodes with even grandparents in a Binary Tree
- Print the nodes of the Binary Tree whose height is a Prime number
- 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
- Count the nodes in the given tree whose weight is prime
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Count of all prime weight nodes between given nodes in the given Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Product of divisors of a number from a given list of its prime factors
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