C++ Version Simple Code


  • -2
    C

    bool isSymAux(TreeNode *node1, TreeNode *node2){
    bool current = false;
    if ((node1 == NULL && node2 != NULL) ||
    (node1 != NULL && node2 == NULL)){ // Reach the wrong one
    return false;
    }
    if (node1 == NULL && node2 == NULL){// Reach the bottom
    return true;
    }
    if (node1->val == node2->val){
    current = true;
    }
    return current&isSymAux(node1->right, node2->left)&isSymAux(node1->left, node2->right);
    }

    bool isSymmetric(TreeNode *root) {
    if (root == NULL) // Checking the root, if it is NULL
    return true;
    return isSymAux(root->left, root->right); // Iterative outcome
    }


Log in to reply
 

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