📖 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 program that creates two binary trees, sets up a helper function to check symmetry, and then uses this function to determine if the two trees are symmetric.
📋 What You'll Learn
Create two binary trees with exact node values
Create a helper function called isMirror that checks if two trees are mirror images
Create a function called areSymmetric that uses isMirror to check symmetry
Print true if the trees are symmetric, otherwise false
💡 Why This Matters
🌍 Real World
Checking if two trees are symmetric is useful in graphics, data validation, and comparing hierarchical data structures.
💼 Career
Understanding tree symmetry helps in coding interviews and software roles involving tree data structures and recursion.
Progress0 / 4 steps
1
Create Two Binary Trees
Create a struct called Node with an int data, and two pointers left and right. Then create two binary trees called tree1 and tree2 with these exact nodes and structure:
tree1: root 1, left child 2, right child 3 tree2: root 1, left child 3, right child 2
DSA C++
Hint
Define the Node struct first. Then create tree1 and tree2 by assigning new nodes with the exact values and structure.
2
Create Helper Function isMirror
Create a function called isMirror that takes two Node* parameters named root1 and root2. This function should return true if the two trees are mirror images of each other, and false otherwise. Use these rules: - If both nodes are nullptr, return true - If one is nullptr and the other is not, return false - Check if root1->data equals root2->data and recursively check root1->left with root2->right and root1->right with root2->left
DSA C++
Hint
Use recursion to compare the nodes and their children in mirror order.
3
Create Function areSymmetric
Create a function called areSymmetric that takes two Node* parameters named root1 and root2. This function should return the result of calling isMirror(root1, root2).
DSA C++
Hint
This function just calls isMirror with the two roots.
4
Print if Trees are Symmetric
Use std::cout to print true if areSymmetric(tree1, tree2) returns true, otherwise print false.
DSA C++
Hint
Use std::cout and the ternary operator to print true or false based on the result of areSymmetric(tree1, tree2).
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: