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 nodes with two children at level L in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
- 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
- Program to count leaf nodes in a binary tree
- Count the number of visible nodes in Binary Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Count half nodes in a Binary tree (Iterative and Recursive)
- Count of nodes in a Binary Tree whose child is its prime factors
- Iterative program to count leaf nodes in a Binary Tree
- Count full nodes in a Binary tree (Iterative and Recursive)
- 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 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.