Clean C++ recursive solution / 3ms

  • 0

    I applied the concept of checking whether trees are same (#100).
    The difference is to check children in the opposite direction at the same time.

    bool checkSymmetric(TreeNode* p, TreeNode* q) {
        if(p && q) {
            if(p->val != q->val) return false;
            return checkSymmetric(p->left,q->right) && checkSymmetric(p->right,q->left);
        return (p == NULL && q == NULL);
    bool isSymmetric(TreeNode* root) {
        return root == NULL || checkSymmetric(root->left, root->right);

Log in to reply

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