题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return bool布尔型
*/
#include <stdbool.h>
bool areSymmetrical(struct TreeNode* pRoot1,struct TreeNode* pRoot2);
bool isSymmetrical(struct TreeNode* pRoot ) {
if(pRoot==NULL)
return true;
return areSymmetrical(pRoot,pRoot);
}
bool areSymmetrical(struct TreeNode* pRoot1,struct TreeNode* pRoot2)
{
if(pRoot1==NULL&&pRoot2!=NULL||pRoot2==NULL&&pRoot1!=NULL)
return false;
if(pRoot1!=NULL&&pRoot2!=NULL&&pRoot1->val!=pRoot2->val)
return false;
if(pRoot1==NULL&&pRoot2==NULL)//都空
return true;
if(pRoot1->val==pRoot2->val&&pRoot1->left==NULL&&pRoot1->right==NULL&&pRoot2->left==NULL&&pRoot2->right==NULL)//不空且值相等
return true;
return areSymmetrical(pRoot1->right,pRoot2->left)&&areSymmetrical(pRoot1->left,pRoot2->right);
}

