What does it mean for two binary trees to be symmetric?
Two binary trees are symmetric if one is the mirror image of the other. This means the left subtree of one tree is the mirror of the right subtree of the other, and vice versa.
Click to reveal answer
beginner
What is the base case when checking if two trees are symmetric?
If both nodes are null, they are symmetric (true). If only one is null, they are not symmetric (false).
Click to reveal answer
intermediate
How do you compare nodes to check symmetry in two trees?
Check if the current nodes have the same value, then recursively check if the left child of one tree is symmetric with the right child of the other tree, and vice versa.
Click to reveal answer
intermediate
Why is recursion useful for checking tree symmetry?
Recursion naturally explores each pair of nodes in the two trees, comparing their values and subtrees in a mirrored way, making the code simple and clear.
Click to reveal answer
intermediate
What is the time complexity of checking if two trees are symmetric?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the comparison.
Click to reveal answer
What should you return if both nodes being compared are null?
Aerror
Bfalse
Cnull
Dtrue
✗ Incorrect
If both nodes are null, they are symmetric by definition.
Which pair of subtrees should be compared to check symmetry?
ALeft subtree of first tree with left subtree of second tree
BRight subtree of first tree with right subtree of second tree
CLeft subtree of first tree with right subtree of second tree
DRoot nodes only
✗ Incorrect
Symmetry means the left subtree of one tree matches the right subtree of the other.
If one node is null and the other is not, what is the result?
ATrees are not symmetric
BTrees are symmetric
CContinue checking
DIgnore null nodes
✗ Incorrect
One null and one non-null node means the trees differ, so not symmetric.
What is the main technique used to check if two trees are symmetric?
AIteration with stack
BRecursion
CSorting nodes
DBreadth-first search
✗ Incorrect
Recursion is the natural way to compare mirrored nodes in two trees.
What is the time complexity of the symmetry check?
AO(n)
BO(log n)
CO(1)
DO(n^2)
✗ Incorrect
Each node is visited once, so the complexity is linear in the number of nodes.
Explain how to check if two binary trees are symmetric using recursion.
Think about comparing left subtree of one tree with right subtree of the other.
You got /3 concepts.
Describe the importance of checking both nodes being null or one being null when determining symmetry.
Consider what happens when you reach the end of branches.
You got /3 concepts.
Practice
(1/5)
1. What does it mean for two binary trees to be symmetric?
easy
A. They are mirror images of each other around their center.
B. They have the same number of nodes.
C. They have the same root value only.
D. They have identical left subtrees only.
Solution
Step 1: Understand symmetry in trees
Two trees are symmetric if one is the mirror reflection of the other, meaning left subtree of one matches right subtree of the other and vice versa.
Step 2: Compare structure and values
Both structure and node values must match in a mirrored way for symmetry.
Final Answer:
They are mirror images of each other around their center. -> Option A
Quick Check:
Symmetry means mirror image = C [OK]
Hint: Symmetric trees mirror each other exactly [OK]
Common Mistakes:
Thinking same number of nodes means symmetric
Only comparing root values
Ignoring subtree structure
2. Which Rust function signature correctly represents a function to check if two binary trees are symmetric?
easy
A. fn is_symmetric(t1: Option<TreeNode>, t2: Option<TreeNode>) -> bool
B. fn is_symmetric(t1: &Option, t2: &Option) -> bool
C. fn is_symmetric(t1: &TreeNode, t2: &TreeNode) -> Option<bool>
D. fn is_symmetric(t1: TreeNode, t2: TreeNode) -> bool
Solution
Step 1: Understand Rust tree node representation
Binary trees in Rust are often represented as Option to handle empty nodes safely.
Step 2: Choose correct signature
Passing references to Option avoids ownership issues and returns bool for symmetric check.
Final Answer:
fn is_symmetric(t1: &Option, t2: &Option) -> bool -> Option B
Quick Check:
Use references to Option and bool return [OK]
Hint: Use references to Option for tree nodes [OK]
Common Mistakes:
Passing TreeNode by value causing ownership errors
Returning Option<bool> instead of bool
Using non-Option types for nullable nodes
3. Given the following Rust code snippet, what will be the output?
A. It compares left subtree with left subtree instead of left with right.
B. It unwraps Option without checking for None.
C. It returns false when both trees are empty.
D. It uses incorrect return type.
Solution
Step 1: Review subtree comparisons
The function compares n1.left with n2.left and n1.right with n2.right, which checks for equality, not mirror symmetry.
Step 2: Identify correct mirror check
For symmetry, n1.left should be compared with n2.right and n1.right with n2.left.
Final Answer:
It compares left subtree with left subtree instead of left with right. -> Option A
Quick Check:
Mirror check swaps left and right subtrees [OK]
Hint: Mirror check swaps left and right subtrees [OK]
Common Mistakes:
Comparing left with left and right with right
Forgetting to check both subtrees recursively
Ignoring None cases
5. You want to check if a single binary tree is symmetric around its center. Which approach correctly uses the two-tree symmetric check function is_mirror to solve this?
hard
A. Call is_mirror(&root, &root) to compare the tree with itself.
B. Call is_mirror(&root.left, &root.left) to compare left subtree with itself.
C. Call is_mirror(&root.left, &root.right) to compare left and right subtrees.
D. Call is_mirror(&root.right, &root.right) to compare right subtree with itself.
Solution
Step 1: Understand single tree symmetry
A tree is symmetric if its left subtree is a mirror of its right subtree.
Step 2: Use is_mirror function correctly
Comparing root.left and root.right subtrees with is_mirror checks this mirror condition.
Final Answer:
Call is_mirror(&root.left, &root.right) to compare left and right subtrees. -> Option C
Quick Check:
Compare left and right subtrees for symmetry [OK]
Hint: Check left and right subtrees for mirror symmetry [OK]
Common Mistakes:
Comparing tree with itself instead of subtrees
Comparing left subtree with itself
Comparing right subtree with itself
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.