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 == NULL)
            return true;
        return isMirrorTree(root->left, root->right);
    }
    
        bool isMirrorTree(TreeNode* p, TreeNode* q) {  
        if(p == NULL && q == NULL)  
            return true;  
        if((p == NULL && q != NULL) || (p != NULL && q == NULL))  
            return false;  
        if(p->val == q->val){  
            if(isMirrorTree(p->left, q->right) && isMirrorTree(p->right, q->left))  
                return true;  
            else  
                return false;  
        }  
        else  
            return false;  
    }  
};
leetcode-100 改版,把递归条件修改,改为检测镜像树即可。我真是机智

猜你喜欢

转载自blog.csdn.net/vigorqq/article/details/80795176