📖 Scenario: Imagine you have two family trees represented as simple binary trees. You want to check if these two trees are mirror images of each other, meaning they are symmetric in structure and values.
🎯 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 using nested objects with val, left, and right properties
Create a helper function called isMirror that takes two nodes and checks if they are mirror images
Create a function called areSymmetric that uses isMirror to check if the two trees are symmetric
Print true or false based on whether the trees are symmetric
💡 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 tree data structures and algorithms.
Progress0 / 4 steps
1
Create Two Binary Trees
Create two binary trees called tree1 and tree2. Each tree node should be an object with val, left, and right properties. Use these exact values:
tree1: root value 1, left child 2, right child 3 tree2: root value 1, left child 3, right child 2
DSA Javascript
Hint
Use nested objects to represent each node with val, left, and right. Use null for empty children.
2
Create Helper Function to Check Mirror
Create a function called isMirror that takes two parameters node1 and node2. It should return true if both nodes are null, or if their values are equal and their left and right children are mirrors of each other. Otherwise, return false.
DSA Javascript
Hint
Check if both nodes are null first. Then check if values match and recursively check children in mirror order.
3
Create Function to Check if Trees are Symmetric
Create a function called areSymmetric that takes two parameters treeA and treeB. It should return the result of calling isMirror(treeA, treeB).
DSA Javascript
Hint
Simply call isMirror with the two trees and return its result.
4
Print if the Two Trees are Symmetric
Use console.log to print the result of calling areSymmetric(tree1, tree2). It should print true if the trees are symmetric, otherwise false.
DSA Javascript
Hint
Use console.log(areSymmetric(tree1, tree2)) to print the result.
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?