![]() |
VOOZH | about |
Given a n-ary tree and a number x, find and return the number of nodes which are greater than x.
Example:
In the given tree, x = 7
Number of nodes greater than x are 4.
Approach: 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
Time complexity: O(n) where n is the number of nodes in the binary tree.
Auxiliary Space: O(h) where h is the height of the binary tree.