6ms c++ solution (dfs recursive)


  • 0
    P
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
     
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            if(!root) return true;
            return isSymmetric(root->left, root->right);
        }
        bool isSymmetric(TreeNode* p, TreeNode* q) {
            if(!p && !q)return true;
            if(p && q) {
                if(p->val != q->val) return false;
            }
            else {
                return false;
            }
            return isSymmetric(p->left, q->right) && isSymmetric(p->right, q->left);
        }
    };
    

Log in to reply
 

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