Help with approach

sol : https://ide.codingblocks.com/s/418011

prob : https://hack.codingblocks.com/app/contests/2022/1085/problem

How do I take the input ? I have written some logic but it’s not working properly

Hey @raghav007 take input like this for the preorder traversal of the tree.

do something like this for building a tree:-

node* buildtree(node *root){
int data, childs;
cin>>data>>childs;

root = new node(data);

if(childs>0){
    root->left = buildtree(root->left);
}
if(childs>1){
    root->right = buildtree(root->right);
}
return root;

}

I tried but not working here is one of my attempt

node* build(){
	int data;
	cin>>data;
	int children;
	cin>>children;

	if(children == 0){
		return NULL;
	}
	node* root = new node(data);

	if(children < 2){
		root->left = build();
	}
	else{
		root->left = build();
		root->right = build();
	}

	return root;
}

Check now ->

node* buildTree()
{
	int d, n;
	cin >> d >> n;

	node *root = new node(d);

	if(n == 1)
	{
		root->left = buildTree();
	}

	if(n == 2)
	{
		root->left = buildTree();
		root->right = buildTree();
	}
	return root;
}

I have built the tree and have made a function for level order traversal and I’m calculating the sum when i reach the required level but it’s not giving the correct answer

Check now -> Have done little bit formatting and have considered data flow, rest your logic was correct.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.