Discover how a simple trick can save you hours of confusing tree comparisons!
Why Check if Two Trees are Symmetric in DSA Javascript?
Imagine you have two family trees drawn on paper, and you want to see if they are mirror images of each other. Doing this by looking at every branch and comparing each node manually can be confusing and tiring.
Manually checking each branch and node is slow and easy to mess up. You might forget to compare some parts or mix up the order, leading to wrong conclusions about whether the trees are symmetric.
Using a simple step-by-step method, we can automatically compare the trees by checking if the left side of one tree matches the right side of the other, and vice versa. This makes the process fast, clear, and error-free.
function areSymmetric(t1, t2) {
// Manually compare nodes one by one
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
if (t1.val !== t2.val) return false;
return areSymmetric(t1.left, t2.right) && areSymmetric(t1.right, t2.left);
}function isSymmetric(root) {
function check(t1, t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
if (t1.val !== t2.val) return false;
return check(t1.left, t2.right) && check(t1.right, t2.left);
}
return check(root.left, root.right);
}This lets us quickly and reliably tell if two trees are mirror images, which helps in many computer tasks like organizing data or checking structures.
Think of checking if two family trees are mirror images to find if two families have symmetrical relationships or patterns, without drawing and comparing every branch by hand.
Manual comparison of trees is slow and error-prone.
Recursive checking of left and right nodes simplifies the process.
Automated symmetry checks save time and avoid mistakes.
Practice
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 CQuick Check:
Symmetry means mirror image = C [OK]
- Thinking same number of nodes means symmetric
- Only comparing root values
- Ignoring structure differences
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 BQuick Check:
Two trees need two parameters = B [OK]
- Using only one tree parameter
- Adding unnecessary third parameter
- Wrong function naming
function isMirror(t1, 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);
}
const tree1 = { val: 1, left: { val: 2, left: null, right: null }, right: { val: 3, left: null, right: null } };
const tree2 = { val: 1, left: { val: 3, left: null, right: null }, right: { val: 2, left: null, right: null } };
console.log(isMirror(tree1, tree2));Solution
Step 1: Check base cases for null nodes
Both trees have matching null nodes at corresponding mirror positions, so base cases return true.Step 2: Compare node values and recursive calls
Root values match (1), left of tree1 (2) matches right of tree2 (2), right of tree1 (3) matches left of tree2 (3). All recursive calls return true.Final Answer:
true -> Option DQuick Check:
Mirror check returns true for symmetric trees [OK]
- Confusing left and right children in recursion
- Ignoring null node base cases
- Assuming order of children doesn't matter
function isMirror(t1, t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1.val === t2.val) && isMirror(t1.left, t2.left) && isMirror(t1.right, t2.right);
}Solution
Step 1: Analyze recursive calls for symmetry
The function compares t1.left with t2.left and t1.right with t2.right, which checks for identical trees, not mirror symmetry.Step 2: Correct recursive calls for mirror check
To check symmetry, t1.left should be compared with t2.right and t1.right with t2.left.Final Answer:
It compares left with left and right with right instead of mirror sides. -> Option AQuick Check:
Mirror check swaps sides in recursion = A [OK]
- Comparing same sides instead of mirror sides
- Missing null checks
- Using assignment (=) instead of equality (===)
Solution
Step 1: Understand iterative symmetry check
Using a queue, we can compare nodes from the left side of one tree with the right side of the other tree iteratively.Step 2: Why other methods fail
Preorder traversal compares nodes in the same order, not mirror order. Height check or root-only check ignores structure and values.Final Answer:
Use a queue to iteratively compare nodes from left of one tree and right of the other. -> Option AQuick Check:
Iterative mirror check uses queue swapping sides = D [OK]
- Using preorder traversal without mirroring
- Checking only height or root values
- Ignoring iterative approach benefits
Ready to prove it?
Pass the test and this pattern moves unknown
