Challenge - 5 Problems
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Mirroring a Simple Binary Tree
What is the output of the following Rust code that mirrors a binary tree and then prints its inorder traversal?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn mirror(root: Option<Rc<RefCell<TreeNode>>>) { if let Some(node) = root { let mut node_borrow = node.borrow_mut(); let left = node_borrow.left.take(); let right = node_borrow.right.take(); node_borrow.left = right; node_borrow.right = left; drop(node_borrow); mirror(node.borrow().left.clone()); mirror(node.borrow().right.clone()); } } fn inorder(root: Option<Rc<RefCell<TreeNode>>>, vals: &mut Vec<i32>) { if let Some(node) = root { inorder(node.borrow().left.clone(), vals); vals.push(node.borrow().val); inorder(node.borrow().right.clone(), vals); } } fn main() { let root = TreeNode::new(1); root.borrow_mut().left = Some(TreeNode::new(2)); root.borrow_mut().right = Some(TreeNode::new(3)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(4)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(5)); mirror(Some(root.clone())); let mut vals = vec![]; inorder(Some(root), &mut vals); println!("{:?}", vals); }
Attempts:
2 left
💡 Hint
Think about how the left and right children are swapped at every node before recursive calls.
✗ Incorrect
Mirroring swaps left and right children at every node. The original inorder traversal is [4, 2, 5, 1, 3]. After mirroring, the tree structure changes, and the inorder traversal becomes [3, 1, 5, 2, 4].
🧠 Conceptual
intermediateUnderstanding Mirror Operation on Binary Trees
Which of the following statements correctly describes the effect of mirroring a binary tree?
Attempts:
2 left
💡 Hint
Focus on what happens to the children of each node during mirroring.
✗ Incorrect
Mirroring means swapping the left and right children of every node in the tree recursively. It does not change node values or remove nodes.
🔧 Debug
advancedIdentify the Bug in Mirror Function
What error will the following Rust code produce when trying to mirror a binary tree?
DSA Rust
fn mirror(root: Option<Rc<RefCell<TreeNode>>>) {
if let Some(node) = root {
let mut node_borrow = node.borrow_mut();
let left = node_borrow.left.clone();
let right = node_borrow.right.clone();
node_borrow.left = right;
node_borrow.right = left;
mirror(node_borrow.left.clone());
mirror(node_borrow.right.clone());
}
}Attempts:
2 left
💡 Hint
Consider Rust's borrowing rules when mutably borrowing and then recursively calling mirror.
✗ Incorrect
The code borrows node mutably and then tries to borrow it again recursively without releasing the mutable borrow, causing a runtime panic due to borrowing rules violation.
❓ Predict Output
advancedOutput After Mirroring a Complex Tree
Given the following binary tree structure and mirror function, what is the preorder traversal output after mirroring?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn mirror(root: Option<Rc<RefCell<TreeNode>>>) { if let Some(node) = root { let mut node_borrow = node.borrow_mut(); let left = node_borrow.left.take(); let right = node_borrow.right.take(); node_borrow.left = right; node_borrow.right = left; drop(node_borrow); mirror(node.borrow().left.clone()); mirror(node.borrow().right.clone()); } } fn preorder(root: Option<Rc<RefCell<TreeNode>>>, vals: &mut Vec<i32>) { if let Some(node) = root { vals.push(node.borrow().val); preorder(node.borrow().left.clone(), vals); preorder(node.borrow().right.clone(), vals); } } fn main() { let root = TreeNode::new(10); root.borrow_mut().left = Some(TreeNode::new(5)); root.borrow_mut().right = Some(TreeNode::new(15)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(3)); root.borrow_mut().left.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(7)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(18)); mirror(Some(root.clone())); let mut vals = vec![]; preorder(Some(root), &mut vals); println!("{:?}", vals); }
Attempts:
2 left
💡 Hint
Preorder traversal visits root, then left subtree, then right subtree. Mirroring swaps left and right children.
✗ Incorrect
After mirroring, left and right children are swapped at every node. The preorder traversal visits nodes in the order: root, right subtree (original left), left subtree (original right).
🧠 Conceptual
expertEffect of Mirroring on Tree Height and Structure
After mirroring a binary tree, which of the following statements is always true?
Attempts:
2 left
💡 Hint
Mirroring only swaps children, it does not add or remove nodes.
✗ Incorrect
Mirroring swaps left and right children at every node but does not change the height or number of nodes. The structure is mirrored but height remains unchanged.
