Help to write the level order sum function

@rajsaxena.personal
See as you need to find the sum for which you need to traverse the tree like we do traverse the array if we need to find the sum of element the array.

So here for traversing the tree you can use any tree traversal like pre, post, inorder or level order traversal, but you need to modify them a little for current requirement.

So i am simply explaining it using preorder traversal

int sum(node * root , int level){
    if(root== NULL){
        return 0;
    }
    ///if at level k, then just need that node, not its child, they will be k+1, so return from here
    if(level==0) {
        return root->data;
    }
    
    //if not reached k, then just go down to its children and find the sum.
    int l = sum(root->left, level-1); 
    int r = sum(root->right, level-1);
    return l+r;   //then return what we got from the childs
} 

I hope i am able to explain the solution with intuition behind it.
Feel free to ask, if something is not clear.
Also please mark this doubt as resolved if you find this helpful.