3. Given the following Rust code for Morris Traversal on a binary tree with nodes 1 (root), 2 (left child), and 3 (right child), what is the printed inorder sequence?
struct Node {
val: i32,
left: Option,
right: Option,
}
fn morris_inorder(root: &mut Option) {
let mut current = root.as_mut();
while let Some(node) = current {
if node.left.is_none() {
print!("{} ", node.val);
current = node.right.as_mut();
} else {
let mut pre = node.left.as_mut();
while let Some(p) = pre {
if p.right.is_some() && p.right.as_ref().unwrap().val != node.val {
pre = p.right.as_mut();
} else {
break;
}
}
if pre.is_some() && pre.as_ref().unwrap().right.is_none() {
pre.as_mut().unwrap().right = Some(Box::new(Node { val: node.val, left: None, right: None }));
current = node.left.as_mut();
} else {
pre.as_mut().unwrap().right = None;
print!("{} ", node.val);
current = node.right.as_mut();
}
}
}
}