why is output always coming as 0
please please explain sir
π‘ Trees -- Find sum at level K
@Shubham_helloworld problem is with the buildTree() function in your code, it is not building the tree correctly, you can check by printing 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;
}
you are genius!!!
1 Like
but can you explain that where in question is it written that if only 1 child is present that will be only left child?
@Shubham_helloworld in the question it is given that βThe input is of preorder form and it is assured that the no of children will not exceed 2β, so you deduce from this that if there are childs for a node then it will first left then right.
1 Like
how this snippet of code is working let suppose we have
20 2
10 0
20 0
can u dry run this for me