Find suma at level k problem

Can You suggest the approcah to take input and form a binary tree from the preroder input given .

Hey @AbhishekAhlawat1102
A Naive approach would be to recursively traverse the tree and add the node’s data to our sum value if it is at the required level. Else recursively call nodes on the lower levels and repeat the process.

Note : In this problem we are given a binary tree only. Only the input format is that of a generic tree. Hence we need not implement a generic tree. We only need to modify the input function to take the input as a generic tree.

i know how to find the sum but i still confused in taking input can u please help with that

@AbhishekAhlawat1102
Did you make the generic tree

no that what i m asking how to make it by the given input , i can then think of the recursive solution

@AbhishekAhlawat1102
public class Main{
static Scanner sc = new Scanner(System.in);

private class Node {
	int data;
	ArrayList<Node> children = new ArrayList<>();
}

private Node root;

public Main() {
	// TODO Auto-generated constructor stub
	this.root = Create_Node(null, -1);

// pass parent Node and ith child

}

private Node Create_Node(Node parent, int ith) {
	int item = sc.nextInt();
	Node node = new Node();
	node.data = item;
//	System.out.println("No of child");
	int noc = sc.nextInt(); // No of child
	for (int i = 0; i < noc; i++) {
		Node child = Create_Node(node, i);
		node.children.add(child);
	}
	return node;
}

}
Dry run this code