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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

## C++

 `// CPP program to find number of nodes  ` `// greater than x ` `#include ` `using` `namespace` `std; ` ` `  `// Structure of a node of n-ary tree ` `struct` `Node { ` `    ``int` `key; ` `    ``vector child; ` `}; ` ` `  `// Utility function to create ` `// a new tree node ` `Node* newNode(``int` `key) ` `{ ` `    ``Node* temp = ``new` `Node; ` `    ``temp->key = key; ` `    ``return` `temp; ` `} ` ` `  `// Function to find nuber of nodes ` `// gretaer than x ` `int` `nodesGreaterThanX(Node* root, ``int` `x) ` `{ ` `    ``if` `(root == NULL) ` `        ``return` `0; ` ` `  `    ``int` `count = 0; ` ` `  `    ``// if current root is greater  ` `    ``// than x increment count ` `    ``if` `(root->key > x) ` `        ``count++; ` ` `  `    ``// Number of children of root ` `    ``int` `numChildren = root->child.size(); ` ` `  `    ``// recursively calling for every child ` `    ``for` `(``int` `i = 0; i < numChildren; i++) { ` `        ``Node* child = root->child[i]; ` `        ``count += nodesGreaterThanX(child, x); ` `    ``} ` ` `  `    ``// return the count ` `    ``return` `count; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``/* Let us create below tree ` `*           5 ` `*         / | \ ` `*        1  2  3 ` `*       /  / \  \ ` `*     15  4   5  6 ` `*/` ` `  `    ``Node* root = newNode(5); ` `    ``(root->child).push_back(newNode(1)); ` `    ``(root->child).push_back(newNode(2)); ` `    ``(root->child).push_back(newNode(3)); ` `    ``(root->child->child).push_back(newNode(15)); ` `    ``(root->child->child).push_back(newNode(4)); ` `    ``(root->child->child).push_back(newNode(5)); ` `    ``(root->child->child).push_back(newNode(6)); ` ` `  `    ``int` `x = 5; ` ` `  `    ``cout << ``"Number of nodes greater than "`  `         ``<< x << ``" are "``; ` `    ``cout << nodesGreaterThanX(root, x)  ` `         ``<< endl; ` ` `  `    ``return` `0; ` `} `

## Python3

 `# Python program to find number of nodes ` `# greater than x ` ` `  `# Structure of a node of n-ary tree ` `class` `Node: ` `    ``def` `__init__(``self``, data): ` `        ``self``.key ``=` `data ` `        ``self``.child ``=` `[] ` ` `  `# Function to find nuber of nodes ` `# gretaer than x ` `def` `nodesGreaterThanX(root: Node, x: ``int``) ``-``> ``int``: ` `    ``if` `root ``is` `None``: ` `        ``return` `0` ` `  `    ``count ``=` `0` ` `  `    ``# if current root is greater ` `    ``# than x increment count ` `    ``if` `root.key > x: ` `        ``count ``+``=` `1` ` `  `    ``# Number of children of root ` `    ``numChildren ``=` `len``(root.child) ` ` `  `    ``# recursively calling for every child ` `    ``for` `i ``in` `range``(numChildren): ` `        ``child ``=` `root.child[i] ` `        ``count ``+``=` `nodesGreaterThanX(child, x) ` ` `  `    ``# return the count ` `    ``return` `count ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``ans ``=` `0` `    ``k ``=` `25` ` `  `    ``# Let us create below tree ` `    ``#           5 ` `    ``#         / | \ ` `    ``#        1  2  3 ` `    ``#       /  / \  \ ` `    ``#      15 4   5  6 ` ` `  `    ``root ``=` `Node(``5``) ` `    ``(root.child).append(Node(``1``)) ` `    ``(root.child).append(Node(``2``)) ` `    ``(root.child).append(Node(``3``)) ` `    ``(root.child[``0``].child).append(Node(``15``)) ` `    ``(root.child[``1``].child).append(Node(``4``)) ` `    ``(root.child[``1``].child).append(Node(``5``)) ` `    ``(root.child[``2``].child).append(Node(``6``)) ` ` `  `    ``x ``=` `5` ` `  `    ``print``(``"Number of nodes greater than %d are %d"` `%` `          ``(x, nodesGreaterThanX(root, x))) ` ` `  `# This code is contributed by ` `# sanjeev2552 `

Output:

```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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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.

My Personal Notes arrow_drop_up

Improved By : sanjeev2552

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.