Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the current node for Morris traversal.
DSA Java
TreeNode current = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing current to null will skip traversal.
Using a variable other than root will cause errors.
✗ Incorrect
The traversal starts from the root node, so current should be initialized to root.
2fill in blank
mediumComplete the code to find the rightmost node in the left subtree.
DSA Java
TreeNode predecessor = current.left; while (predecessor.right != null && predecessor.right != [1]) { predecessor = predecessor.right; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking against null only misses the thread condition.
Using root instead of current causes incorrect checks.
✗ Incorrect
We check if predecessor.right is not pointing back to current to avoid infinite loops.
3fill in blank
hardFix the error in the code that creates the temporary thread to current.
DSA Java
if (predecessor.right == null) { predecessor.right = [1]; current = current.left; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning predecessor.right to null breaks the thread.
Assigning predecessor.right to predecessor causes a cycle.
✗ Incorrect
The thread should point from predecessor.right to current to revisit current later.
4fill in blank
hardFill both blanks to remove the temporary thread and move current forward.
DSA Java
else { predecessor.right = [1]; System.out.print(current.val + " "); current = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Not removing the thread causes infinite loops.
Moving current to current.left repeats nodes.
✗ Incorrect
Remove the thread by setting predecessor.right to null, then move current to its right child.
5fill in blank
hardFill the blanks to print current's value and move current when left child is null.
DSA Java
if (current.left == null) { System.out.print([1] + " "); current = [2]; } else { // handle left subtree }
Attempts:
3 left
💡 Hint
Common Mistakes
Printing current instead of current.val prints object reference.
Moving current to current.left causes infinite loops.
✗ Incorrect
Print current.val and move current to current.right when no left child exists.
