0
0
DSA C++programming~20 mins

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

Choose your learning style9 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.