edited by
341 views
2 votes
2 votes

Consider a C function print() which takes a root of binary search tree and a positive number $‘k’$ as arguments as given below:

struct node
{
    int data;
    struct node *left, *right;
};
int count = 0;
void print(struct node *root, int k)
{
    if (root!=NULL && count < k)
    {
        print(root -> right, k);
        count++;
        if(count == k)
            printf("%d", root -> data);
        print(root -> left, k);
    }
}

What is the sum of all the values output by print(root, $2$), where root represents root of the following Binary Search Tree? (You can assume the binary search tree to be terminated properly by NULLs)

edited by

1 Answer

Best answer
3 votes
3 votes
The code will stop printing value when the value of $k$ will be equal to count. Initially the value of count is $0$ its value will become $1$ at node $7$ and will become $2$ at node $6$ hence will print the value $6$.
selected by
Answer:

Related questions

5 votes
5 votes
1 answer
3