How to take this kind of a input ?
Trees -- Find sum at level K
@vaishnavtannu
You can refer to this function.
node *buildTree(node *root){
int d, n;
cin >> d >> n;
root = new node(d);
if (n == 0)
{
return root;
}
else if (n == 1)
{
root->left = buildTree(root->left);
return root;
}
else
{
root->left = buildTree(root->left);
root->right = buildTree(root->right);
return root;
}}
why we are passing root as the parameter ? Can we build tree with passing root parameter ?
and what will be this root while calling this function ?
@vaishnavtannu
Initially that will be NULL.
node *buildTree(node *root){
int d, n;
cin >> d >> n;
root = new node(d);
if (n == 0)
{
return root;
}
else if (n == 1)
{
root->left = buildTree(root->left);
return root;
}
else
{
root->left = buildTree(root->left);
root->right = buildTree(root->right);
return root;
}}int main(){
node *root = NULL;
root= buildTree(root);
int k;
cin >> k;
traverseTree(root, k);
cout << sum << endl;
return 0;
}