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 email@example.com. 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.
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.
- 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
- 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
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Print all pairs from two BSTs whose sum is greater than the given value
- Count of nodes that are greater than Ancestors
- Node having maximum number of nodes less than its value in its subtree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Sum of previous numbers that are greater than current number for given array
- Queries to count array elements greater than or equal to a given number with updates
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- 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
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print levels with odd number of nodes and even number of nodes
- Smallest number in BST which is greater than or equal to N
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)