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 trees are empty (null), they are symmetric. If only one is empty, they are not symmetric.
Click to reveal answer
intermediate
How do you compare nodes when checking for symmetry?
Compare the values of the current nodes. Then recursively check if the left child of the first tree is symmetric with the right child of the second tree, and the right child of the first tree is symmetric with the left child of the second tree.
Click to reveal answer
intermediate
Why is recursion a good approach to check tree symmetry?
Because symmetry involves comparing subtrees in a mirrored way, recursion naturally handles this by breaking the problem into smaller symmetric checks on child nodes.
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 you return if both input trees are null when checking symmetry?
ATrue
BFalse
CNull
DError
✗ Incorrect
If both trees are empty, they are symmetric by definition.
When comparing two nodes for symmetry, which children do you compare?
ALeft child of first tree with left child of second tree
BRight child of first tree with right child of second tree
CLeft child of first tree with right child of second tree
DNone of the above
✗ Incorrect
Symmetry means left subtree of one matches right subtree of the other.
If the root values of two trees differ, are the trees symmetric?
AYes
BNo
COnly if subtrees are symmetric
DDepends on tree height
✗ Incorrect
Root values must be equal for trees to be symmetric.
What is the main technique used to check if two trees are symmetric?
ARecursion
BIteration
CSorting
DHashing
✗ Incorrect
Recursion is used to compare mirrored subtrees.
What is the worst-case time complexity to check if two trees with n nodes are symmetric?
AO(n^2)
BO(log n)
CO(1)
DO(n)
✗ Incorrect
Each node is visited once, so time complexity is O(n).
Explain how to check if two binary trees are symmetric using recursion.
Think about mirror images and how left and right children relate.
You got /5 concepts.
Describe the time complexity and why it is that way when checking two trees for symmetry.
Consider how many nodes you check and how often.
You got /4 concepts.
Practice
(1/5)
1. What does it mean for two binary trees to be symmetric?
easy
A. They have the same root value only.
B. They have the same number of nodes.
C. They are mirror images of each other around their center.
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 around the center.
Step 2: Compare structure and node 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 C
Quick Check:
Symmetry means mirror image = A [OK]
Hint: Symmetric trees mirror each other perfectly [OK]
Common Mistakes:
Thinking same number of nodes means symmetric
Only comparing root values
Ignoring structure when checking symmetry
2. Which of the following C++ function signatures correctly represents a function to check if two trees are symmetric?
easy
A. int isSymmetric(TreeNode* root1, TreeNode* root2);
B. bool isSymmetric(TreeNode* root1, TreeNode* root2);
C. void isSymmetric(TreeNode root1, TreeNode root2);
D. bool isSymmetric(TreeNode root1, TreeNode root2);
Solution
Step 1: Identify correct return type
The function should return a boolean indicating symmetry, so return type is bool.
Step 2: Use pointers for tree nodes
Tree nodes are usually passed as pointers to avoid copying and to handle null nodes.
Final Answer:
bool isSymmetric(TreeNode* root1, TreeNode* root2); -> Option B
Quick Check:
Return bool and use pointers = C [OK]
Hint: Use bool return and pointers for tree nodes [OK]
Common Mistakes:
Using void return type
Passing nodes by value instead of pointer
Using int instead of bool for result
3. What will be the output of the following code snippet?
#include <iostream>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
int main() {
TreeNode* root1 = new TreeNode(1);
root1->left = new TreeNode(2);
root1->right = new TreeNode(3);
TreeNode* root2 = new TreeNode(1);
root2->left = new TreeNode(3);
root2->right = new TreeNode(2);
if (isMirror(root1, root2))
std::cout << "Symmetric";
else
std::cout << "Not Symmetric";
return 0;
}
medium
A. Symmetric
B. Not Symmetric
C. Compilation Error
D. Runtime Error
Solution
Step 1: Analyze tree structures
root1 has left=2, right=3; root2 has left=3, right=2, which are mirror images.
Step 2: Trace isMirror function
Values match and recursive calls check left-right and right-left subtrees, confirming symmetry.
Final Answer:
Symmetric -> Option A
Quick Check:
Mirror check returns true = Symmetric [OK]
Hint: Check mirrored node values and subtree positions [OK]
Common Mistakes:
Confusing left and right subtree checks
Ignoring null node base cases
Assuming same structure means symmetric
4. Identify the bug in the following code that checks if two trees are symmetric: