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 tree, and vice versa.
Click to reveal answer
beginner
What is the base case when checking if two trees are symmetric?
If both trees are empty (NULL), they are symmetric. If only one is empty, they are not symmetric.
Click to reveal answer
intermediate
Which recursive checks are needed to confirm two trees are symmetric?
Check if the root values are equal, then recursively check if the left subtree of the first tree is symmetric with the right subtree of the second tree, and the right subtree of the first tree is symmetric with the left subtree of the second tree.
Click to reveal answer
beginner
Why do we compare left subtree of one tree with right subtree of the other tree?
Because symmetry means mirror image, so the left side of one tree should match the right side of the other tree to be symmetric.
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 in the trees, because each node is visited once during the comparison.
Click to reveal answer
What should be returned if both input trees are NULL when checking symmetry?
ATrue
BFalse
CDepends on tree size
DError
✗ Incorrect
Two empty trees are symmetric by definition.
When checking symmetry, which subtree pairs are compared?
ALeft with left, right with right
BNo subtree comparison needed
CLeft with right, right with left
DRoot with root only
✗ Incorrect
Symmetry means mirror image, so left subtree of one tree is compared with right subtree of the other.
If the root values of two trees differ, what can we conclude?
ATrees are identical
BTrees are symmetric
CCheck subtrees to decide
DTrees are not symmetric
✗ Incorrect
Root values must be equal for trees to be symmetric.
What is the main technique used to check if two trees are symmetric?
AIterative traversal
BRecursion
CSorting nodes
DHashing
✗ Incorrect
Recursion is used to compare subtrees in a mirror fashion.
What is the worst-case time complexity of checking symmetry for two trees with n nodes?
AO(n)
BO(log n)
CO(1)
DO(n^2)
✗ Incorrect
Each node is visited once, so time complexity is linear O(n).
Explain how to check if two binary trees are symmetric using recursion.
Think about mirror images and how left and right subtrees relate.
You got /4 concepts.
Describe why comparing left subtree of one tree with right subtree of the other is necessary for symmetry check.
Imagine folding one tree over the other.
You got /4 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 roots
B. They have the same number of nodes
C. They have the same preorder traversal
D. They have the same height
Solution
Step 1: Understand symmetry in trees
Two trees are symmetric if one is the mirror reflection of the other around their roots.
Step 2: Compare with other options
Having the same number of nodes, preorder traversal, or height does not guarantee mirror symmetry.
Final Answer:
They are mirror images of each other around their roots -> Option A
Quick Check:
Symmetry means mirror image = A [OK]
Hint: Symmetric trees mirror each other around the root [OK]
Common Mistakes:
Confusing same size with symmetry
Thinking same traversal means symmetric
Assuming same height means symmetric
2. Which of the following C function signatures is correct to check if two trees are symmetric?
easy
A. int isSymmetric(struct Node* root1, struct Node* root2);
B. void isSymmetric(struct Node root1, struct Node root2);
C. int isSymmetric(struct Node root1, struct Node root2);
D. int isSymmetric(Node* root1, Node root2);
Solution
Step 1: Understand pointer usage in C for trees
Tree nodes are accessed via pointers, so parameters must be pointers to struct Node.
Step 2: Analyze each option
int isSymmetric(struct Node* root1, struct Node* root2); uses pointers correctly and returns int for boolean result. Options A and B pass structs by value, which is inefficient and incorrect. int isSymmetric(Node* root1, Node root2); mixes pointer and non-pointer types incorrectly.
Final Answer:
int isSymmetric(struct Node* root1, struct Node* root2); -> Option A
Quick Check:
Use pointers for tree nodes in C function [OK]
Hint: Use pointers to struct Node for tree parameters [OK]
Common Mistakes:
Passing structs by value instead of pointers
Mixing pointer and non-pointer parameters
Using void return type instead of int for boolean
3. What will be the output of the following code snippet checking symmetry of two trees?
Both nodes have value 1 and no children, so both left and right pointers are NULL.
Step 2: Evaluate recursive calls
isMirror returns 1 if both nodes are NULL (true). Since both left and right children are NULL, recursive calls return 1. Values match, so overall returns 1.
Final Answer:
1 -> Option B
Quick Check:
Identical single nodes are symmetric = 1 [OK]
Hint: Single nodes with same value are symmetric [OK]
Common Mistakes:
Confusing NULL children as asymmetric
Expecting 0 for identical nodes
Misreading recursive base cases
4. Identify the bug in this function to check if two trees are symmetric:
A. Should use OR instead of AND in return statement
B. Missing check for NULL nodes
C. Return type should be void
D. Should compare t1->left with t2->right and t1->right with t2->left
Solution
Step 1: Understand mirror symmetry check
Mirror symmetry requires left subtree of one tree to match right subtree of the other and vice versa.
Step 2: Identify incorrect recursive calls
The code compares left with left and right with right, which checks for identical trees, not mirror symmetry.
Final Answer:
Should compare t1->left with t2->right and t1->right with t2->left -> Option D
Quick Check:
Mirror means cross compare left and right subtrees [OK]
Hint: Mirror check swaps left and right subtrees [OK]
Common Mistakes:
Comparing left with left and right with right
Ignoring NULL checks
Using wrong logical operators
5. Given two binary trees, how would you implement a function to check if they are symmetric using a queue (iterative approach)? Which step is essential in this approach?
hard
A. Compare only root nodes and ignore children
B. Traverse both trees in preorder and compare arrays
C. Enqueue pairs of nodes to compare left of one with right of other
D. Use a stack to store nodes from only one tree
Solution
Step 1: Understand iterative symmetry check
Use a queue to store pairs of nodes to compare their values and subtrees iteratively.
Step 2: Identify correct node pairing
Pairs must be left child of one tree with right child of the other to check mirror symmetry.
Step 3: Reject incorrect approaches
Preorder traversal comparison or using stack for one tree only does not check mirror symmetry. Ignoring children misses the point.
Final Answer:
Enqueue pairs of nodes to compare left of one with right of other -> Option C
Quick Check:
Queue pairs left-right nodes for iterative mirror check [OK]
Hint: Queue pairs left and right nodes to check symmetry iteratively [OK]
Common Mistakes:
Comparing same side children instead of opposite
Using preorder traversal arrays
Ignoring children nodes in comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.