面试题28:对称的二叉树
题目:请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
struct BinaryTreeNode{
double value;
BinaryTreeNode* left;
BinaryTreeNode* right;
};
bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2){
if(pRoot1==NULL && pRoot2==NULL) return true;
if(pRoot1==NULL || pRoot2==NULL) return false;
if(pRoot1->value!=pRoot2->value) return false;
return isSymmetrical(pRoot1->left, pRoot2->right)&&isSymmetrical(pRoot1->right, pRoot2->left);
}
bool isSymmetrical(BinaryTreeNode* pRoot){
return isSymmetrical(pRoot, pRoot);
}
int main() {
return 0;
}