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(Node* root) {
if (root == nullptr) return;
postorder(root->[1]);
// Continue traversal
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling postorder on root->right first.
Calling postorder on root->parent which does not exist.
Using an incorrect pointer name.
✗ 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(Node* root) {
if (root == nullptr) return;
postorder(root->left);
postorder(root->[1]);
// Continue traversal
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling postorder on root->left again.
Using root->parent which is not part of traversal.
Using an invalid pointer name.
✗ 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 code to print the root node's value after traversing left and right subtrees.
DSA C++
void postorder(Node* root) {
if (root == nullptr) return;
postorder(root->left);
postorder(root->right);
std::cout << root->[1] << " ";
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing root->left or root->right which are pointers.
Using root->value which may not exist.
Forgetting to print the root node.
✗ Incorrect
The root node's data is stored in the 'data' member, so we print root->data after traversing children.
4fill in blank
hardFill both blanks to complete the postorder traversal function that returns a vector of node values.
DSA C++
void postorder(Node* root, std::vector<int>& result) {
if (root == nullptr) return;
postorder(root->[1], result);
postorder(root->[2], result);
result.push_back(root->data);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right child pointers.
Using 'parent' or 'child' which are invalid pointers.
Forgetting to pass 'result' vector.
✗ Incorrect
Postorder visits left child first, then right child, so blanks are 'left' and 'right'.
5fill in blank
hardFill all three blanks to create a postorder traversal that returns a map from node data to its depth in the tree.
DSA C++
void postorder(Node* root, std::map<int, int>& depthMap, int depth) {
if (root == nullptr) return;
postorder(root->[1], depthMap, depth + 1);
postorder(root->[2], depthMap, depth + 1);
depthMap[root->[3]] = depth;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'data' for node value.
Swapping left and right pointers.
Not incrementing depth correctly.
✗ Incorrect
Traverse left and right children first, then map root->data to current depth.