📖 Scenario: Imagine you have two family trees represented as binary trees. You want to check if these two trees are mirror images of each other, meaning they are symmetric around their center.
🎯 Goal: Build a TypeScript program that creates two binary trees, sets a helper variable, writes a function to check if the two trees are symmetric, and finally prints the result.
📋 What You'll Learn
Create two binary trees using the TreeNode class with exact values
Create a helper variable called result initialized to true
Write a function called isSymmetric that takes two TreeNode parameters and returns a boolean
Use recursion inside isSymmetric to check if the trees are symmetric
Print the final result which is true if symmetric, otherwise false
💡 Why This Matters
🌍 Real World
Checking if two trees are symmetric is useful in graphics, data organization, and comparing hierarchical data structures.
💼 Career
Understanding tree symmetry helps in coding interviews and software roles involving data structures and algorithms.
Progress0 / 4 steps
1
Create two binary trees
Create a class called TreeNode with val, left, and right properties. Then create two trees called tree1 and tree2 with these exact nodes:
tree1: root value 1, left child 2, right child 3 tree2: root value 1, left child 3, right child 2
DSA Typescript
Hint
Define the TreeNode class first. Then create tree1 with root 1, left 2, right 3. Create tree2 with root 1, left 3, right 2.
2
Add a helper variable
Create a boolean variable called result and set it to true. This will store if the trees are symmetric or not.
DSA Typescript
Hint
Just write let result = true; below the tree creation.
3
Write the symmetric check function
Write a function called isSymmetric that takes two parameters node1 and node2 of type TreeNode | null. Use recursion to check if the two trees are mirror images. Return true if symmetric, otherwise false. Use these rules: - If both nodes are null, return true - If one is null and the other is not, return false - If values differ, return false - Recursively check node1.left with node2.right and node1.right with node2.left
DSA Typescript
Hint
Use the rules given to write the recursive function. Then assign result by calling isSymmetric(tree1, tree2).
4
Print the result
Print the value of the variable result to show if the two trees are symmetric or not.
DSA Typescript
Hint
Use console.log(result); to print the final answer.
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 root.
B. They have the same number of nodes.
C. They have the same values in the same order.
D. They are both balanced binary search trees.
Solution
Step 1: Understand symmetry in trees
Two trees are symmetric if one is the mirror reflection of the other around the root node.
Step 2: Differentiate from other properties
Having the same number of nodes or values in order does not guarantee symmetry; symmetry requires mirrored structure and values.
Final Answer:
They are mirror images of each other around their root. -> Option A
Quick Check:
Symmetry means mirror image = A [OK]
Hint: Symmetric trees mirror each other around the root [OK]
Common Mistakes:
Confusing symmetry with identical trees
Thinking same node count means symmetry
Assuming balanced trees are always symmetric
2. Which TypeScript function signature correctly represents a function to check if two trees are symmetric?
easy
A. function symmetricCheck(root1: TreeNode, root2: TreeNode): void
B. function isSymmetric(root1: TreeNode, root2: TreeNode): boolean
C. function checkSymmetry(tree1: TreeNode, tree2: TreeNode): number
D. function isSymmetric(root: TreeNode): boolean
Solution
Step 1: Identify correct parameters and return type
The function should take two tree roots and return a boolean indicating symmetry.
Step 2: Check function signature correctness
function isSymmetric(root1: TreeNode, root2: TreeNode): boolean matches this: two TreeNode parameters and boolean return type.
Final Answer:
function isSymmetric(root1: TreeNode, root2: TreeNode): boolean -> Option B
Quick Check:
Two roots + boolean return = D [OK]
Hint: Symmetry check needs two roots and boolean output [OK]
Common Mistakes:
Using only one root parameter
Returning number instead of boolean
Using void return type
3. What will be the output of the following TypeScript code snippet?
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function isMirror(t1: TreeNode | null, t2: TreeNode | null): boolean {
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 = new TreeNode(1, new TreeNode(2), new TreeNode(3));
const tree2 = new TreeNode(1, new TreeNode(3), new TreeNode(2));
console.log(isMirror(tree1, tree2));
medium
A. SyntaxError
B. false
C. true
D. RuntimeError
Solution
Step 1: Understand the isMirror function logic
It checks if two trees are mirror images by comparing root values and recursively checking left of one with right of other and vice versa.
Step 2: Trace the given trees
tree1 has root 1, left 2, right 3; tree2 has root 1, left 3, right 2. They are mirrors, so isMirror returns true.
Final Answer:
true -> Option C
Quick Check:
Mirror check returns true for these trees [OK]
Hint: Mirror check compares opposite children recursively [OK]
Common Mistakes:
Confusing left and right child comparison
Assuming same structure means mirror
Ignoring null checks
4. Identify the bug in this TypeScript function meant to check if two trees are symmetric:
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
if (t1.val !== t2.val) return false;
return isSymmetric(t1.left, t2.left) && isSymmetric(t1.right, t2.right);
}
medium
A. It does not check node values before recursion.
B. It misses the base case when both nodes are null.
C. It returns true when one node is null and the other is not.
D. It compares left with left and right with right instead of mirror sides.
Solution
Step 1: Analyze recursive calls
The function compares t1.left with t2.left and t1.right with t2.right, which checks identical structure, not mirror symmetry.
Step 2: Identify correct mirror comparison
For 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 D
Quick Check:
Mirror needs cross child comparison = A [OK]
Hint: Mirror check swaps left and right children in recursion [OK]
Common Mistakes:
Comparing same side children instead of opposite
Forgetting to check node values before recursion
Missing null base cases
5. Given two binary trees, write a TypeScript function that returns true if they are symmetric around their roots, and false otherwise. Which approach correctly solves this problem?
hard
A. Use a helper function to recursively compare left subtree of one tree with right subtree of the other and vice versa.
B. Traverse both trees in preorder and compare the traversal arrays for equality.
C. Check if both trees have the same height and node count.
D. Compare only the root node values of both trees.
Solution
Step 1: Understand symmetry check requirements
Symmetry means the left subtree of one tree is the mirror of the right subtree of the other.
Step 2: Choose correct approach
Recursively comparing left subtree of one with right subtree of the other ensures mirror symmetry is checked properly.
Final Answer:
Use a helper function to recursively compare left subtree of one tree with right subtree of the other and vice versa. -> Option A