Trees-find sum at level k

What is wrong with this function? What is the correct solution?
public static void sumat(Node n,int k){
int level=-1,sum=0;
LinkedList queue=new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
Node rn=queue.remove();

            sum=sum+n.data;
        
        if(rn.left!=null){
            queue.add(rn.left);
        }
        if(rn.right!=null){
            queue.add(rn.right);
        }
                }
    System.out.println(sum);
}

Hii Harseerat,
Your code will simply print the sum of all nodes of the tree.

Correct solution of this problem is:
public static int kthlevelsum(Node node, int k) {

        int lvl = 0;
        
        int sum = 0;
        
        LinkedList<Node> Tree = new LinkedList<>();

	LinkedList<Node> HTree = new LinkedList<>();

	Tree.addLast(node);

	while (!Tree.isEmpty()) {
		Node temp = Tree.removeFirst();
		
		if(lvl == k){
		    sum += temp.data;
		}

		for (Node child : temp.children) {
			HTree.addLast(child);
		}

		if (Tree.isEmpty()) {
			lvl++;
			Tree = HTree;
			HTree = new LinkedList<>();
		}
	}
	
	return sum;
    }

What is temp.children in the code you provided?

Temp is any node in the given tree and by temp.children I am accessing the children of that node and then I add them to the tree

Hey Harseerat, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.