Complete the code to declare a simple array of integers.
int arr[5] = [1];
The correct syntax to initialize an array in C++ uses curly braces {}.
Complete the code to create a linked list node struct with an integer value and a pointer to the next node.
struct Node {
int data;
[1] next;
};& instead of pointer *.int* instead of Node*.The pointer to the next node must be of type Node* to link nodes.
Fix the error in the tree node constructor to initialize the value and set left and right children to nullptr.
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left([1]), right([2]) {}
};NULL instead of nullptr.0 which is less clear.In modern C++, nullptr is used to represent a null pointer safely.
Fill both blanks to create a function that returns the number of children of a binary tree node.
int countChildren(TreeNode* node) {
int count = 0;
if (node->left [1] nullptr) count++;
if (node->right [2] nullptr) count++;
return count;
}We check if left or right child is not equal to nullptr to count existing children.
Fill all three blanks to create a function that builds a linked list from an array of integers.
Node* buildList(int arr[], int size) {
if (size == 0) return [1];
Node* head = new Node();
head->data = arr[0];
head->next = buildList(arr + 1, [2]);
return [3];
}NULL instead of nullptr.Return nullptr for empty list, reduce size by 1 for recursion, and return head node.