Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to move to the left child in Morris traversal.
DSA C
if (current->left == NULL) { printf("%d ", current->data); current = current->[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child instead of right child when left is NULL.
Trying to move to parent which is not directly accessible.
✗ Incorrect
In Morris traversal, when the left child is NULL, we print the current node and move to its right child.
2fill in blank
mediumComplete the code to find the inorder predecessor of the current node.
DSA C
predecessor = current->left; while (predecessor->right != NULL && predecessor->right != current) { predecessor = predecessor->[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child instead of right child in the loop.
Not checking if predecessor->right equals current.
✗ Incorrect
To find the inorder predecessor, we move to the rightmost node in the left subtree.
3fill in blank
hardFix the error in setting the temporary link from predecessor to current node.
DSA C
if (predecessor->right == NULL) { predecessor->right = [1]; current = current->left; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning predecessor->right to current->left instead of current.
Assigning NULL which breaks the traversal.
✗ Incorrect
We set predecessor->right to current to create a temporary link for Morris traversal.
4fill in blank
hardFill both blanks to remove the temporary link and move to the right child.
DSA C
else { predecessor->[1] = [2]; printf("%d ", current->data); current = current->right; }
Attempts:
3 left
💡 Hint
Common Mistakes
Removing link from left pointer instead of right.
Setting link to current instead of NULL.
✗ Incorrect
We remove the temporary link by setting predecessor->right to NULL and then move to current's right child.
5fill in blank
hardFill all three blanks to complete the Morris inorder traversal loop.
DSA C
while (current != NULL) { if (current->left == NULL) { printf("%d ", current->data); current = current->[1]; } else { predecessor = current->left; while (predecessor->right != NULL && predecessor->right != current) { predecessor = predecessor->[2]; } if (predecessor->right == NULL) { predecessor->right = [3]; current = current->left; } else { predecessor->right = NULL; printf("%d ", current->data); current = current->right; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing left and right pointers in the traversal.
Setting predecessor->right to NULL instead of current when creating thread.
✗ Incorrect
The traversal moves to current->right when left is NULL, moves predecessor to right child, and sets predecessor->right to current to create the thread.
