Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the postorder traversal function recursively on the left child.
DSA C
void postorder(struct Node* root) {
if (root == NULL) return;
postorder(root->[1]);
postorder(root->right);
printf("%d ", root->data);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using root->right instead of root->left for the first recursive call.
Trying to access root->data instead of a child pointer.
✗ Incorrect
In postorder traversal, we first visit the left subtree, so we call postorder on root->left.
2fill in blank
mediumComplete the code to call the postorder traversal function recursively on the right child.
DSA C
void postorder(struct Node* root) {
if (root == NULL) return;
postorder(root->left);
postorder(root->[1]);
printf("%d ", root->data);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using root->left instead of root->right for the second recursive call.
Trying to print root->right instead of calling postorder on it.
✗ Incorrect
In postorder traversal, after visiting the left subtree, we visit the right subtree by calling postorder on root->right.
3fill in blank
hardFix the error in the print statement to correctly print the node's data.
DSA C
void postorder(struct Node* root) {
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%[1]d ", root->data);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using %f or %s which causes incorrect output or runtime errors.
Omitting the % sign or using wrong specifiers.
✗ Incorrect
The correct format specifier for printing an integer in C is %d.
4fill in blank
hardFill both blanks to complete the postorder traversal function that prints nodes in left-right-root order.
DSA C
void postorder(struct Node* root) {
if (root == [1]) return;
postorder(root->[2]);
postorder(root->right);
printf("%d ", root->data);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking root == root instead of root == NULL.
Calling postorder on root->right first instead of root->left.
✗ Incorrect
The base case checks if root is NULL, and the first recursive call is on root->left.
5fill in blank
hardFill all three blanks to complete the postorder traversal function with correct base case, recursive calls, and print statement.
DSA C
void postorder(struct Node* root) {
if (root == [1]) return;
postorder(root->[2]);
postorder(root->[3]);
printf("%d ", root->data);
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right child calls order.
Printing before recursive calls instead of after.
✗ Incorrect
The base case is root == NULL, then recursive calls on root->left and root->right, then print root->data.
