题目描述
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot)
{
if(pRoot == nullptr)
return true;
return compareNode(pRoot->left, pRoot->right);
}
bool compareNode(TreeNode* lRoot, TreeNode* rRoot) {
if(lRoot == nullptr && rRoot == nullptr)
return true;
if(lRoot == nullptr || rRoot == nullptr)
return false;
if(lRoot->val == rRoot->val)
return compareNode(lRoot->left, rRoot->right) && compareNode(lRoot->right, rRoot->left);
else
return false;
}
};