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.
- Count greater nodes in AVL tree
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Count the number of nodes at given level in a tree using BFS.
- Minimum number of nodes in an AVL Tree with given height
- Relationship between number of nodes and height of binary tree
- Delete nodes which have a greater value on right side
- Delete all the nodes from the list that are greater than x
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete linked list nodes which have a greater value on left side
- Transform a BST to greater sum tree
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Subtree of all nodes in a tree using DFS
- Sum of all nodes in a binary tree
- Sum of all leaf nodes of binary tree