My AC code which can reach 13ms


  • 0
    3
    class Solution {
    public:
        bool helper(TreeNode *l, TreeNode *r){
            //if r and l equal NULL, they equal each other,
            //it is risky but cann't pass
            if(r == l) return true;
            if(l == NULL || r == NULL) return false;
            if(l->val != r->val) return false;
            return helper(l->left, r->right) && helper(l->right, r->left);
        }
        bool isSymmetric(TreeNode *root) {
            if(root == NULL) return true;
            else return helper(root->left, root->right);
        }
        
    };
    

    it can reach 13ms. When r and l equal NULL, they are the same, which is the same as r == NULL && l == NULL


  • 0
    R

    //it is risky but cann't pass
    if(r == l) return true;

    Why do you say this is risky ?


Log in to reply
 

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