101 symmetric tree

递归判断左右子树是否对称,
对称的实现只有当满足左右树都存在,同时保持的值都相同,而且LL==RR LR==RL,其他情况都是false

bool em(struct TreeNode *left, struct TreeNode * right)
{
    if(left == NULL && right == NULL)
        return true;
    else if(left != NULL && right == NULL)
        return false;
    else if(left == NULL && right != NULL)
        return false;
    else{
        if(left->val == right->val)
            return em(left->left,right->right)&&em(left->right, right->left);
        else
            return false;
    }

}
bool isSymmetric(struct TreeNode* root) {

    if(root == NULL)
        return true;

    return em(root->left, root->right);
}

你可能感兴趣的:(101 symmetric tree)