Bird
Raised Fist0
DSA C++programming~20 mins

Check if Two Trees are Symmetric in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of symmetric check for two trees
What is the output of the following C++ code that checks if two binary trees are symmetric?
DSA C++
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);
}

bool isSymmetric(TreeNode* root) {
    if (!root) return true;
    return isMirror(root->left, root->right);
}

int main() {
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(2);
    root->left->left = new TreeNode(3);
    root->left->right = new TreeNode(4);
    root->right->left = new TreeNode(4);
    root->right->right = new TreeNode(3);

    bool result = isSymmetric(root);
    std::cout << (result ? "true" : "false") << std::endl;
    return 0;
}
Atrue
Bfalse
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check if the left subtree is a mirror of the right subtree.
❓ Predict Output
intermediate
2:00remaining
Output when trees are not symmetric
What is the output of the following C++ code that checks if two binary trees are symmetric?
DSA C++
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);
}

bool isSymmetric(TreeNode* root) {
    if (!root) return true;
    return isMirror(root->left, root->right);
}

int main() {
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(2);
    root->left->right = new TreeNode(3);
    root->right->right = new TreeNode(3);

    bool result = isSymmetric(root);
    std::cout << (result ? "true" : "false") << std::endl;
    return 0;
}
Afalse
Btrue
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check if the left subtree is a mirror of the right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in symmetric tree check
What error does the following C++ code produce when checking if two trees are symmetric?
DSA C++
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);
}

bool isSymmetric(TreeNode* root) {
    if (!root) return true;
    return isMirror(root->left, root->right);
}

int main() {
    TreeNode* root = nullptr;
    bool result = isSymmetric(root);
    std::cout << (result ? "true" : "false") << std::endl;
    return 0;
}
AOutputs 'false'
BSegmentation fault (runtime error)
CCompilation error
DOutputs 'true'
Attempts:
2 left
💡 Hint
Check if the root is null before accessing its children.
🧠 Conceptual
advanced
1:30remaining
Understanding symmetry in binary trees
Which of the following statements about symmetric binary trees is TRUE?
AA tree is symmetric if all nodes have the same value.
BA tree is symmetric if its left subtree is identical to its right subtree.
CA tree is symmetric if its left subtree is a mirror reflection of its right subtree.
DA tree is symmetric if it has the same number of nodes on left and right subtrees.
Attempts:
2 left
💡 Hint
Symmetry means mirror image, not identical structure.
❓ Predict Output
expert
2:30remaining
Output of complex symmetric tree check with null nodes
What is the output of the following C++ code that checks if two binary trees are symmetric?
DSA C++
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);
}

bool isSymmetric(TreeNode* root) {
    if (!root) return true;
    return isMirror(root->left, root->right);
}

int main() {
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(2);
    root->left->left = nullptr;
    root->left->right = new TreeNode(3);
    root->right->left = nullptr;
    root->right->right = new TreeNode(3);

    bool result = isSymmetric(root);
    std::cout << (result ? "true" : "false") << std::endl;
    return 0;
}
Atrue
BRuntime error
CCompilation error
Dfalse
Attempts:
2 left
💡 Hint
Check if the left subtree is a mirror of the right subtree including null nodes.

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

  1. Step 1: Understand symmetry in trees

    Two trees are symmetric if one is the mirror reflection of the other around the center.
  2. Step 2: Compare structure and node values

    Both structure and node values must match in a mirrored way for symmetry.
  3. Final Answer:

    They are mirror images of each other around their center. -> Option C
  4. 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

  1. Step 1: Identify correct return type

    The function should return a boolean indicating symmetry, so return type is bool.
  2. Step 2: Use pointers for tree nodes

    Tree nodes are usually passed as pointers to avoid copying and to handle null nodes.
  3. Final Answer:

    bool isSymmetric(TreeNode* root1, TreeNode* root2); -> Option B
  4. 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

  1. Step 1: Analyze tree structures

    root1 has left=2, right=3; root2 has left=3, right=2, which are mirror images.
  2. Step 2: Trace isMirror function

    Values match and recursive calls check left-right and right-left subtrees, confirming symmetry.
  3. Final Answer:

    Symmetric -> Option A
  4. 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:
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->left) && isMirror(t1->right, t2->right);
}
medium
A. It uses incorrect return type.
B. It does not check for null nodes.
C. It returns true when both nodes are null.
D. It compares left subtree with left subtree instead of left with right.

Solution

  1. Step 1: Review recursive calls for symmetry

    Symmetry requires comparing left subtree of one tree with right subtree of the other.
  2. Step 2: Identify incorrect subtree comparisons

    The code compares left with left and right with right, which checks for identical trees, not symmetric.
  3. Final Answer:

    It compares left subtree with left subtree instead of left with right. -> Option D
  4. Quick Check:

    Mirror needs left-right swap, code misses it = D [OK]
Hint: Mirror check swaps left and right subtrees [OK]
Common Mistakes:
  • Comparing same side subtrees instead of mirrored sides
  • Ignoring null checks
  • Confusing identical and symmetric trees
5. Given two binary trees, write a function that returns true if they are symmetric around their center. Which approach correctly solves this problem?
hard
A. Use a recursive function that compares left subtree of first tree with right subtree of second tree and vice versa.
B. Traverse both trees in preorder and compare node values in order.
C. Check if both trees have the same number of nodes and same root value.
D. Use a queue to perform level order traversal and compare nodes at each level without mirroring.

Solution

  1. Step 1: Understand symmetry check logic

    Symmetry means one tree is mirror of the other, so compare left subtree of one with right subtree of other recursively.
  2. Step 2: Evaluate other approaches

    Preorder traversal or node count alone do not guarantee symmetry; level order without mirroring misses mirror structure.
  3. Final Answer:

    Use a recursive function that compares left subtree of first tree with right subtree of second tree and vice versa. -> Option A
  4. Quick Check:

    Recursive mirror comparison solves symmetry = A [OK]
Hint: Recursively compare left with right subtree for symmetry [OK]
Common Mistakes:
  • Using simple traversal without mirroring
  • Comparing only node counts or root values
  • Ignoring subtree structure in mirror form
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard