Sum at level K Trees

How to build this tree? I am not able to get the input format

Hello @Kishan-Mishra-2464674013761441,

Let’s understand this with an example:
Input:
1 2
2 2
3 0
4 0
5 2
6 0
7 0

1 2 means 1 has two child.
2 2 means 1 has first child 2 and 2 has 2 child
3 0 means 2 has first child 3 and 3 has 0 child
4 0 means 2 has second child 4 and 4 has 0 child
5 2 means 1 has second child 5 and 5 has 2 child
6 0 means 5 has first child 6 and 6 has 0 child
7 0 means 5 has second child 7 and 7 has 0 child

Tree at level 0 : 1
Tree at level 1 : 2 5
Tree at level 2 : 3 4 6 7

Visualisation:
____________1
______2___________5
___3_____4_____6_____7

Hope, this would help.
Give a like, if you are satisfied.

Thanks that was really helpful, but how do we take input in this tree. Like for calculating the sum at level β€˜k’, my approach is that after building the tree, i will use the printgivenlevel function(recursive level order) and pass level β€˜k’ as the parameter and instead of printing the nodes at level k, i’ll just take their sum.
But for that how am i supposed to build this tree. I understood your explanation, but are we going to use a Queue for building this or what?

Hello @Kishan-Mishra-2464674013761441,

You can refer to the following segment of code for the implementation of what i have explained in my previous reply:

Give a like, if you are satisfied.

1 Like