Leetcode-关于二叉树的对称性

二叉树的镜像对称问题,是一个easy的题
/**
 * 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==NULL) return true;
        if(root->left==NULL&&root->right==NULL) return true;
        return is(root->left,root->right);
    }
    bool is(TreeNode* root1,TreeNode* root2)
    {
        bool r,l;
        if(root1==root2&&root1==NULL) return true;
        else if(root1==NULL||root2==NULL) return false;
        //else if(root1->left==NULL&&root1->right==NULL&&root2->left==NULL&&root2->right==NULL) return true;
        else if(root1->val==root2->val) return is(root1->left,root2->right)&&is(root1->right,root2->left);
        else return false;
    }
};

你可能感兴趣的:(Leetcode-关于二叉树的对称性)