1. 程式人生 > >leetcode 101. Symmetric Tree

leetcode 101. Symmetric Tree

/**
 * 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==nullptr)
            return true;
        
        return issymmetric(root->left,root->right);
    }
    
    bool issymmetric(TreeNode* r1,TreeNode* r2) // **
    {
        if(r1==nullptr&&r2==nullptr) //(!r1 && !r2)
            return true;
        if(r1==nullptr||r2==nullptr)
            return false;
        
        if(r1->val!=r2->val)
            return false;
        
        return issymmetric(r1->left,r2->right)&&issymmetric(r1->right,r2->left);
    }
    
    
};