Share my 3ms C solution


  • 1

    more codes see:https://github.com/lightmen/leetcode.git

    int is_symmetric(struct TreeNode *left, struct TreeNode *right)
    {

    if(!left && !right)
        return 1;
    if(!left || !right || left->val != right->val)
        return 0;
    return is_symmetric(left->right,right->left) 
        & is_symmetric(left->left,right->right);
    

    }

    bool isSymmetric(struct TreeNode *root) {

    if(!root)
        return true;
    return is_symmetric(root->left,root->right);
    

    }


  • 0
    H

    optimized condition

    {
    if(left && right)
        return left->val == right->val && is_symmetric(left->right,right->left) && is_symmetric(left->left,right->right);
    
    // either left or right should be null, so it's easy to judge.
    return left == right;
    }

Log in to reply
 

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