Based on the binary tree below, what would the output be of the function call mystery(root)?
struct treenode {
int data;
struct treenode* left;
struct treenode* right;
}
void mystery(struct treenode* root) {
if (root == NULL) return;
mystery(root->right);
mystery(root->left);
if (root->data%2 == 0) {
root->data /= 2;
}
else {
int sum = 0;
if (root->left != NULL) sum += root->left->data;
if (root->right != NULL) sum += root->right->data;
root->data += sum;
}
printf(“%d “, root->data);
}
Binary Tree: 63 | 47 16 | 86 32 NULL 9 | NULL NULL 95 NULL NULL NULL 53 64 |
This is my understanding of the function:
mystery(63->right)
mystery(63->left)
Then it's going to check to see if root->data (63) is odd or else Since it's odd, then
sum += root->left(47)
sum += root->right(16)
root->data(63) += sum,
so now sum = ?
And then it's going to recursively call mystery(46) and mystery(16)
Is this the right idea?