Complete the code to declare a struct for a binary tree node.
struct Node {
int data;
struct Node* [1];
struct Node* right;
};The left child pointer of a binary tree node is conventionally named left.
Complete the code to create a new node with given data.
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->[1] = NULL;
return node;
}The right child pointer must be initialized to NULL when creating a new node.
Fix the error in the function to update the bottom view map with the current node's data.
void updateBottomViewMap(int hd, int data, int* map, int* visited) {
if (visited[hd] == 0) {
map[hd] = data;
visited[hd] = 1;
} else {
map[hd] = [1];
}
}For bottom view, we update the map with the current node's data to overwrite previous nodes at the same horizontal distance.
Fill both blanks to perform recursive traversal and update horizontal distance for left and right children.
void bottomViewUtil(struct Node* root, int hd, int* map, int* visited, int* minHd, int* maxHd) {
if (root == NULL) return;
updateBottomViewMap(hd, root->data, map, visited);
if (hd < *minHd) *minHd = [1];
if (hd > *maxHd) *maxHd = [2];
bottomViewUtil(root->left, hd - 1, map, visited, minHd, maxHd);
bottomViewUtil(root->right, hd + 1, map, visited, minHd, maxHd);
}hd - 1 or hd + 1 instead of hd for min/max updates.The minimum and maximum horizontal distances are updated with the current horizontal distance hd.
Fill all three blanks to print the bottom view from min to max horizontal distance.
void printBottomView(int* map, int minHd, int maxHd) {
for (int i = [1]; i <= [2]; i++) {
printf("%d ", map[[3]]);
}
printf("\n");
}The loop runs from minHd to maxHd, printing the map values at each horizontal distance i.
