Given a n-ary tree, count number of nodes which have more number of children than parents
Given a N-ary tree represented as adjacency list, we need to write a program to count all such nodes in this tree which has more number of children than its parent.
For Example,

In the above tree, the count will be 1 as there is only one such node which is ‘2’ which has more number of children than its parent. 2 has three children (4, 5 and 6) whereas its parent, 1 has only two children (2 and 3).
We can solve this problem using both BFS and DFS algorithms. We will explain here in details about how to solve this problem using BFS algorithm.
As the tree is represented using adjacency list representation. So, for any node say ‘u’ the number of children of this node can be given as adj[u].size().
Now the idea is to apply BFS on the given tree and while traversing the children of a node ‘u’ say ‘v’ we will simply check is adj[v].size() > adj[u].size().
Below is the implementation of above idea:
CPP
// C++ program to count number of nodes // which has more children than its parent #include<bits/stdc++.h> using namespace std; // function to count number of nodes // which has more children than its parent int countNodes(vector<int> adj[], int root) { int count = 0; // queue for applying BFS queue<int> q; // BFS algorithm q.push(root); while (!q.empty()) { int node = q.front(); q.pop(); // traverse children of node for( int i=0;i<adj[node].size();i++) { // children of node int children = adj[node][i]; // if number of childs of children // is greater than number of childs // of node, then increment count if (adj[children].size() > adj[node].size()) count++; q.push(children); } } return count; } // Driver program to test above functions int main() { // adjacency list for n-ary tree vector<int> adj[10]; // construct n ary tree as shown // in above diagram adj[1].push_back(2); adj[1].push_back(3); adj[2].push_back(4); adj[2].push_back(5); adj[2].push_back(6); adj[3].push_back(9); adj[5].push_back(7); adj[5].push_back(8); int root = 1; cout << countNodes(adj, root); return 0; } |
Python3
# Python3 program to count number of nodes # which has more children than its parent from collections import deque adj = [[] for i in range(100)] # function to count number of nodes # which has more children than its parent def countNodes(root): count = 0 # queue for applying BFS q = deque() # BFS algorithm q.append(root) while len(q) > 0: node = q.popleft() # traverse children of node for i in adj[node]: # children of node children = i # if number of childs of children # is greater than number of childs # of node, then increment count if (len(adj[children]) > len(adj[node])): count += 1 q.append(children) return count # Driver program to test above functions # construct n ary tree as shown # in above diagram adj[1].append(2) adj[1].append(3) adj[2].append(4) adj[2].append(5) adj[2].append(6) adj[3].append(9) adj[5].append(7) adj[5].append(8) root = 1 print(countNodes(root)) # This code is contributed by mohit kumar 29 |
Output:
1
Time Complexity: O( n ) , where n is the number of nodes in the tree.
This article is contributed by Harsh Agarwal. 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Count nodes with two children at level L in a Binary Tree
- Count the number of nodes at given level in a tree using BFS.
- Number of children of given node in n-ary Tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Number of nodes greater than a given value in n-ary tree
- Number of special nodes in an n-ary tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Count the nodes whose sum with X is a Fibonacci number
- Relationship between number of nodes and height of binary tree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Count all pairs of adjacent nodes whose XOR is an odd number
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Print levels with odd number of nodes and even number of nodes
- Number of full binary trees such that each node is product of its children

