C++ Simple Recursion, O(N) Time O(1) Space


  • 0
    M
    class Solution {
    public:
        bool twoMirrored(TreeNode const * a, TreeNode const * b) {
            if (!a || !b) return !a && !b;
            return a->val == b->val && twoMirrored(a->left, b->right) && twoMirrored(a->right, b->left);        
        }
        bool isSymmetric(TreeNode* root) {
            return !root || twoMirrored(root->left, root->right);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.