【LeetCode刷题】101.对称二叉树

题目链接

101. 对称二叉树 - 力扣(LeetCode)

实现代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool isSymmetrictree(struct TreeNode* p, struct TreeNode* q)
{
     //两棵子树都为空
    if(p == NULL && q == NULL)
        return true;
    //一棵子树为空,一棵子树不为空
    if(p == NULL || q == NULL)
        return false;
    //两棵子树的根的值相同则比较子树的子树
    if(p->val != q->val)
        return false;
    return isSymmetrictree(p->left, q->right) && isSymmetrictree(p->right, q->left);
}

bool isSymmetric(struct TreeNode* root)
{
    if(NULL == root)
        return true;
    return isSymmetrictree(root->left, root->right);
}

你可能感兴趣的:(java,算法,数据结构)