Find Sum at level K

How to take input? what is the base case when to end taking input? The questions are designed poorly.

Hello Rohit, sorry if you feel the question is not appropriate.
Let me explain you how we are taking the input :
1 2
2 2
3 0
4 0
5 2
6 0
7 0
2

This is the input it means that we have first node whose value is 1 and then it has two children.
so 1 is the tree till now
Now the second node is 2 and again having two children.
So tree till now is
1
/
2
Now the third node is 3 and having 0 children so the tree will be till here,
1
/
2
/
3
Please notice we are going in preorder format. Now 3 has no children so the next node is 4 and having 0 children.
So the tree will be
1
/
2
/
3 4
Now the next node is 5 and it has also two children moreover the second node got all its children, and 3rd, 4th don’t have any children so we will move on to the first node and insert this 5th node as its second child. So the tree will be like
1
/ \
2 5
/ \
3 4
similarly following the remaining steps we get the overall tree as,
1
/ \
2 5
/ \ / \
3 4 6 7
Now the tree is completed and we need to tell the sum of the nodes that are at the second level so it is 20 as 3 + 4 + 6 + 7 = 20
I hope it is clear to you. In case it is clear to you pls mark it as resolve and provide the rating as well as feedback so that we can improve ourselves.
In case there is still some confusion pls let me know, I will surely try to help you out.
Thanks :slight_smile:
Happy Coding !!