剑指Offer——面试题28:对称的二叉树

面试题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;
}
发布了42 篇原创文章 · 获赞 43 · 访问量 1064

猜你喜欢

转载自blog.csdn.net/qq_35340189/article/details/104410129