Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to move to the left child in Morris traversal.
DSA Rust
while current.is_some() { let mut node = current.unwrap(); if node.left.is_none() { print!("{} ", node.val); current = node.[1]; } else { // Find predecessor } }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child instead of right when left is None.
Trying to access parent pointer which may not exist.
✗ Incorrect
In Morris traversal, when left child is None, we print current node and move to its right child.
2fill in blank
mediumComplete the code to find the inorder predecessor in Morris traversal.
DSA Rust
let mut predecessor = node.left.as_ref().unwrap(); while predecessor.right.is_some() && predecessor.right.as_ref() != Some(&node) { predecessor = predecessor.[1].as_ref().unwrap(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child instead of right child to find predecessor.
Not checking if predecessor's right points back to current node.
✗ Incorrect
The inorder predecessor is found by moving to the rightmost node in left subtree.
3fill in blank
hardFix the error in setting the temporary link from predecessor to current node.
DSA Rust
if predecessor.right.is_none() { predecessor.right = Some(Box::new(node.[1])); current = node.left.clone(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a clone of left child instead of current node.
Using val instead of node reference.
✗ Incorrect
We must set predecessor.right to point to current node itself, not a clone of its child.
4fill in blank
hardFill both blanks to remove the temporary link and move to the right child.
DSA Rust
else { predecessor.right = [1]; print!("{} ", node.val); current = node.[2].clone(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting predecessor.right to Some instead of None.
Moving to left child instead of right child after removal.
✗ Incorrect
We remove the temporary link by setting predecessor.right to None and move to current's right child.
5fill in blank
hardFill all three blanks to complete the Morris inorder traversal loop.
DSA Rust
while let Some(node) = current.clone() { if node.left.is_none() { print!("{} ", node.val); current = node.[1].clone(); } else { let mut predecessor = node.left.as_ref().unwrap(); while predecessor.right.is_some() && predecessor.right.as_ref() != Some(&node) { predecessor = predecessor.[2].as_ref().unwrap(); } if predecessor.right.is_none() { predecessor.right = Some(Box::new(node.clone())); current = node.[3].clone(); } else { predecessor.right = None; print!("{} ", node.val); current = node.right.clone(); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing left and right pointers in traversal.
Not cloning nodes properly causing ownership errors.
✗ Incorrect
Move to right child when no left child; find predecessor by moving right; set current to left child after linking.
