3. What is the output of the following code snippet counting nodes in a binary tree?
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int countNodes(Node* root) {
if (root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
// Tree structure:
// 10
// / \
// 5 15
// / / \
// 3 12 20
int main() {
Node n3 = {3, NULL, NULL};
Node n5 = {5, &n3, NULL};
Node n12 = {12, NULL, NULL};
Node n20 = {20, NULL, NULL};
Node n15 = {15, &n12, &n20};
Node root = {10, &n5, &n15};
printf("%d", countNodes(&root));
return 0;
}