What's wrong with my C code


  • -1
    C

    bool issame(struct TreeNode* l, struct TreeNode* r){
    struct TreeNode* llson=l->left;
    struct TreeNode* lrson=l->right;
    struct TreeNode* rlson=r->left;
    struct TreeNode* rrson=r->right;
    if(l==NULL&&r==NULL) return true;
    if(l->val==r->val&&llson==NULL&&lrson==NULL&&rlson==NULL&&rrson==NULL) return true;
    if(l->val!=r->val) return false;
    if(l!=NULL&&r==NULL) return false;
    if(l==NULL&&r!=NULL) return false;
    return issame(llson,rrson)&&issame(lrson,rlson);
    }

    bool isSymmetric(struct TreeNode* root) {
    if(root==NULL) return true;
    if(root->left==NULL&&root->right==NULL) return true;
    return issame(root->left,root->right);
    }


  • 0
    B

    root->left could be NULL and root->right is not NULL
    In that case you will try to access root->left->left and fail.


Log in to reply
 

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