PLease help in the following ques
Please tell me how to take input int this
You can build the tree like this
Node* buildTree() {
int rt, child;
cin >> rt >> child;
Node *root = new Node (rt);
if (child == 0)
return root;
root->left = buildTree();
if (child == 2)
root->right = buildTree();
return root;
}
Logic to find sum at level k
- use a level variable to determine the level of current node
- call on left and right part of tree and store ans of both part
return the sum of both ans - if current level is equal to k return root->data ( value of node)
Complete Reference Code