VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-nodes-greater-given-value-n-ary-tree/

⇱ Number of nodes greater than a given value in n-ary tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number of nodes greater than a given value in n-ary tree

Last Updated : 13 Mar, 2023

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

👁 tree

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. 


Output
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.

Comment
Article Tags: