Given a n-ary tree and a number x, find and return the number of nodes which are greater than x.
In the given tree, x = 7 Number of nodes greater than x are 4.
The idea is maintain a count variable initialize to 0. Traverse the tree and compare root data with x. If root data is greater than x, increment the count variable and recursively call for all its children.
Below is the implementation of idea.
Number of nodes greater than 5 are 2
This article is contributed by Chhavi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Number of unique paths in tree such that every path has a value greater than K
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Number of special nodes in an n-ary tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Count the number of nodes at given level in a tree using BFS.
- Relationship between number of nodes and height of binary tree
- Count the nodes in the given Tree whose weight is a Perfect Number
- Count the nodes in the given tree whose weight is a powerful number
- Count nodes in the given tree whose weight is a fibonacci number
- Count the number of visible nodes in Binary Tree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Least Common Ancestor of any number of nodes in Binary Tree
- Print the nodes of the Binary Tree whose height is a Prime number
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count of all prime weight nodes between given nodes in the given Tree
- Count of nodes that are greater than Ancestors
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
Improved By : sanjeev2552