Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

Symmetric Tree 🌟

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

O(N) Time solution

Code

class Solution {
public:
    bool isSymmetric(TreeNode* root){
        return root == NULL || isSymmetricHelp(root->left, root->right);
    }

    bool isSymmetricHelp(TreeNode* left, TreeNode* right){
        if (left == NULL || right == NULL)
            return left == right;
        if (left->val != right->val)
            return false;

        return isSymmetricHelp(left->left, right->right) && isSymmetricHelp(left->right, right->left);
    }
};

O(N) Time, using 2 queue, iterative solution

class Solution {
public:
    bool isSymmetric(TreeNode* root){
        if (!root) return true;

        TreeNode *left, *right;
        queue<TreeNode*> q1, q2;
        q1.push(root->left);
        q2.push(root->right);

        while (!q1.empty() && !q2.empty()) {
            left = q1.front();
            q1.pop();
            right = q2.front();
            q2.pop();

            if (left == NULL && right == NULL) continue;
            if (left == NULL || right == NULL) return false;
            if (left->val != right->val) return false;

            q1.push(left->left);
            q1.push(left->right);
            q2.push(right->right);
            q2.push(right->left);
        }
        return true;
    }
};

O(N) Time, using 1 queue, iterative solution

class Solution{
public:
    bool isSymmetric(TreeNode *root){
        TreeNode *left, *right;
        if (!root) return true;

        queue<TreeNode *> q;
        q.push(root->left);
        q.push(root->right);

        while (!q.empty()){
            left = q.front();
            q.pop();
            right = q.front();
            q.pop();

            if (left == NULL && right == NULL) continue;
            if (left == NULL || right == NULL) return false;
            if (left->val != right->val) return false;

            q.push(left->left);
            q.push(right->right);
            q.push(left->right);
            q.push(right->left);
        }
        return true;
    }
};