6ms c++ dfs solution


  • 0
    E

    class Solution {
    public:
    bool isSymmetric(TreeNode* root) {
    if(root==NULL)return true;
    return isSame(root->left,root->right);
    }

    bool isSame(TreeNode* p,TreeNode* q){
        if(p==NULL&&q==NULL)return true;
        if((p==NULL &&q!=NULL) ||(q==NULL &&p!=NULL)||(q->val!=p->val))return false;
        if(!isSame(p->left,q->right))return false;
        return isSame(p->right,q->left);
        
    }
    

    };


Log in to reply
 

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