# Number of children of given node in n-ary Tree | GeeksforGeeks

Hello friends!

And, welcome to another tutorial on GeeksforGeeks. In this video we are going to understand the

program which finds Number of children of given node in n-ary Tree

. First, let us take an example. 50 has 3 children having values 40, 100 and

20. …Now, let us see the approach..

Now, let us see the algorithm. Let us also have a sample tree to test our

algorithm. We pass the root node which is 1 to function

numberofchildren.. So, root will point to 1. Aso, let us assume that we have to find

the number of children for node 2. So, we pass 2 to x.

Next, we take a numChildren variable which we initialize to 0

Since root is not null, we take a queue and push the root node which is 1 into the

queue. Since the queue is not empty, we enter the

while loop and now, n will be equal to the number of elements in the queue. Since,

there is only one element in the queue, n will be equal to 1.

Since, n is>0, we enter the inner while loop and now, we take a node p which will

point to q’s front that is 1. Next, we pop an element from the queue.

Now, we check if p’s key is equal to x. As it is false, we enqueue all the children

of the dequeued item that is 1. So, 2 3 5 and

6 will be enqueeud We decrement n. So, n will now be 0

Since n is 0, we break out of the inner while loop and continue with the outer while

loop. Now, n will be 4 as there are 4 elements in the queue.

Again as n is greater than 0, we enter the inner while loop and now, p will point to

2 and 2 will be popped Since p is equal to x, the if condition is

satisfied and we set numChildren to numChildren + adjacency list size of 2 which

stores the number of children that is 1. So, numChildren will be 1.

We return numChildren that is 1 to the caller module. And thus, number of

children of 2 is 1.WIth this we come to an end of this tutorial.

For any doubts or suggestions please leave them in the comment section below.

Thanks for watching!

how to print sum the children values and add to the parent node as result for every parent