Complete the code to declare the struct for a binary tree node.
typedef struct TreeNode {
int val;
struct TreeNode *[1];
struct TreeNode *right;
} TreeNode;The left child pointer in a binary tree node is usually named left.
Complete the code to initialize the queue for level order traversal.
TreeNode* queue[100]; int front = 0, rear = 0; queue[rear++] = [1];
We start the level order traversal by adding the root node to the queue.
Fix the error in the condition to check if the queue is empty.
while (front [1] rear) { TreeNode* node = queue[front++]; // process node }
The queue is not empty while front is less than rear.
Fill both blanks to add the right child to the queue if it exists.
if (node->[1] != NULL) { queue[rear++] = node->[2]; }
We check if the right child exists and then add it to the queue.
Fill all three blanks to store the rightmost node's value at each level.
int rightSideView[100]; int size = 0; while (front < rear) { int levelSize = rear - front; for (int i = 0; i < levelSize; i++) { TreeNode* node = queue[front++]; if (i == [1] - 1) { rightSideView[size++] = node->[2]; } if (node->[3] != NULL) queue[rear++] = node->right; if (node->left != NULL) queue[rear++] = node->left; } }
The rightmost node is at index levelSize - 1, we store its value, and add its right child to the queue.
