Find sum at kth level

Take a generic tree input where you are first given the data of the node and then its no of children. The input is of preorder form and it is assured that the no of children will not exceed 2. The input of the tree is followed by a single integer K.
************how to take this kind of input for making a tree??
**********I have written the function for calculating sum as below…plz check if it is fine??

hi @ayu2321

  1. since it is mentioned that the number of child nodes can be 2 at max, you can consider it as a binary tree only and then construct the tree.
  2. This function is not correct as you are not taking the sum of left child and right chikd into account (you called the function, they will return values, but you are not storing thema anywhere and not returning those values also, so they will not be counted.) Instead of returning value of ans, you can take a reference variable also, that will be much simpler.