Complete the code to declare a struct for a binary tree node.
typedef struct Node {
int data;
struct Node* [1];
struct Node* right;
} Node;The left child pointer of the binary tree node is named left.
Complete the code to create a new node with given data.
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->[1] = NULL;
return newNode;
}The right child pointer is named right and should be initialized to NULL.
Fix the error in the function to insert nodes in vertical order traversal map.
void insertVertical(Node* root, int hd, map<int, vector<int>>* m) {
if (root == NULL) return;
(*m)[hd].push_back(root->[1]);
insertVertical(root->left, hd - 1, m);
insertVertical(root->right, hd + 1, m);
}We need to insert the node's data value into the map, not the left or right pointers.
Fill both blanks to complete the vertical order traversal function.
void verticalOrderTraversal(Node* root) {
map<int, vector<int>> m;
int [1] = 0;
insertVertical(root, [2], &m);
for (auto it : m) {
for (int val : it.second) {
printf("%d ", val);
}
printf("\n");
}
}The horizontal distance variable is commonly named hd and initialized to 0.
Fill all three blanks to complete the main function that builds the tree and calls vertical order traversal.
int main() {
Node* root = createNode([1]);
root->left = createNode([2]);
root->right = createNode([3]);
verticalOrderTraversal(root);
return 0;
}The root node is 1, left child is 2, and right child is 3 to form a simple binary tree.
