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
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 allows checking smaller parts of the trees step-by-step until reaching the base case.
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?
AError
BFalse
CNull
DTrue
✗ 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 right child of second tree
BRight child of first tree with right child of second tree
CLeft child of first tree with left child of second tree
DNone of the above
✗ Incorrect
Symmetry means left subtree of one tree matches right subtree of the other.
If one tree node is null and the other is not, are the trees symmetric?
AOnly if values match
BYes
CNo
DDepends on tree height
✗ Incorrect
One null and one non-null node means trees differ, so not symmetric.
What is the main technique used to check if two trees are symmetric?
ARecursion
BSorting
CIteration
DHashing
✗ Incorrect
Recursion helps compare subtrees in mirrored order easily.
What is the time complexity of checking symmetry for two trees with n nodes?
AO(1)
BO(n)
CO(log n)
DO(n^2)
✗ Incorrect
Each node is visited once, so time complexity is linear in number of nodes.
Explain step-by-step how to check if two binary trees are symmetric.
Think about mirror image and recursion base cases.
You got /6 concepts.
Describe why recursion is suitable for checking tree symmetry and what the base cases are.
Consider how trees are structured and how recursion explores them.
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 = C [OK]
Hint: Symmetric trees mirror each other exactly [OK]
Common Mistakes:
Thinking same number of nodes means symmetric
Only comparing root values
Ignoring structure differences
2. Which of the following is the correct JavaScript function signature to check if two trees are symmetric?
easy
A. function checkSymmetric(tree) {}
B. function isSymmetric(tree1, tree2) {}
C. function symmetricCheck(tree1, tree2, tree3) {}
D. function isSymmetric(tree1) {}
Solution
Step 1: Identify parameters needed
To check symmetry between two trees, we need two tree inputs.
Step 2: Match function signature
Only the function with two parameters for two trees matches the requirement.
Final Answer:
function isSymmetric(tree1, tree2) {} -> Option B
Quick Check:
Two trees need two parameters = B [OK]
Hint: Two trees need two parameters in function [OK]
Common Mistakes:
Using only one tree parameter
Adding unnecessary third parameter
Wrong function naming
3. What will be the output of the following code snippet?