Challenge - 5 Problems
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of symmetric tree check function
What is the output of the following Rust code that checks if two binary trees are symmetric?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug, PartialEq, Eq)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Self { TreeNode { val, left: None, right: None } } } fn is_symmetric(t1: Option<Rc<RefCell<TreeNode>>>, t2: Option<Rc<RefCell<TreeNode>>>) -> bool { match (t1, t2) { (None, None) => true, (Some(n1), Some(n2)) => { let n1 = n1.borrow(); let n2 = n2.borrow(); n1.val == n2.val && is_symmetric(n1.left.clone(), n2.right.clone()) && is_symmetric(n1.right.clone(), n2.left.clone()) }, _ => false, } } fn main() { let left = Rc::new(RefCell::new(TreeNode::new(2))); left.borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(3)))); left.borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(4)))); let right = Rc::new(RefCell::new(TreeNode::new(2))); right.borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(4)))); right.borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(3)))); println!("{}", is_symmetric(Some(left), Some(right))); }
Attempts:
2 left
💡 Hint
Check if the values and the mirrored children match recursively.
✗ Incorrect
The function recursively compares the values of nodes and their mirrored children. The two trees are symmetric because left subtree of one matches right subtree of the other and vice versa.
❓ Predict Output
intermediateOutput when one tree is None
What is the output of the following Rust code when checking symmetry between a tree and None?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug, PartialEq, Eq)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Self { TreeNode { val, left: None, right: None } } } fn is_symmetric(t1: Option<Rc<RefCell<TreeNode>>>, t2: Option<Rc<RefCell<TreeNode>>>) -> bool { match (t1, t2) { (None, None) => true, (Some(n1), Some(n2)) => { let n1 = n1.borrow(); let n2 = n2.borrow(); n1.val == n2.val && is_symmetric(n1.left.clone(), n2.right.clone()) && is_symmetric(n1.right.clone(), n2.left.clone()) }, _ => false, } } fn main() { let tree = Some(Rc::new(RefCell::new(TreeNode::new(1)))); let none_tree: Option<Rc<RefCell<TreeNode>>> = None; println!("{}", is_symmetric(tree, none_tree)); }
Attempts:
2 left
💡 Hint
One tree is None, the other is not.
✗ Incorrect
The function returns false if one tree is None and the other is not, so output is false.
🔧 Debug
advancedIdentify the error in symmetric check function
What error will this Rust code produce when checking symmetry of two trees?
DSA Rust
fn is_symmetric(t1: Option<Rc<RefCell<TreeNode>>>, t2: Option<Rc<RefCell<TreeNode>>>) -> bool { match (t1, t2) { (None, None) => true, (Some(n1), Some(n2)) => { let n1 = n1.borrow(); let n2 = n2.borrow(); n1.val == n2.val && is_symmetric(n1.left.clone(), n2.left.clone()) && is_symmetric(n1.right.clone(), n2.right.clone()) }, _ => false, } }
Attempts:
2 left
💡 Hint
Check if the recursive calls compare mirrored children correctly.
✗ Incorrect
The code compares left with left and right with right, not mirrored children, so it returns false for symmetric trees.
🧠 Conceptual
advancedWhich property must hold for two trees to be symmetric?
For two binary trees to be symmetric, which condition must be true?
Attempts:
2 left
💡 Hint
Think about mirror image structure and values.
✗ Incorrect
Symmetry means the left subtree of one tree matches the right subtree of the other and vice versa, with equal root values.
🚀 Application
expertNumber of recursive calls for symmetric check on balanced tree
Given a balanced binary tree of height h (root at height 0), how many total calls to is_symmetric are made when checking if the tree is symmetric with itself?
Attempts:
2 left
💡 Hint
Each call compares a pair of nodes; the number of nodes in a full binary tree is 2^(h+1)-1.
✗ Incorrect
The function compares pairs of nodes recursively. For a balanced tree of height h, total nodes are 2^(h+1)-1, so that many calls are made.
