面试题28:对称的二叉树

#include<iostream>

struct BinaryTreeNode
{
	int m_nValue;
	BinaryTreeNode* pLeft, * pRight;
	BinaryTreeNode(int _value):m_nValue(_value),pLeft(nullptr),pRight(nullptr){}
};
//利用树的遍历性质,前遍历、中遍历、后遍历都是先左节点再右节点
//设计一个遍历,从右遍历到左遍历,然后对比两个遍历组序
bool isSymmetrical(BinaryTreeNode* root1, BinaryTreeNode* root2)
{
	if (root1 == nullptr && root2 == nullptr)return true;//如果两个比较的节点都为空也对称
	if (root1 == nullptr || root2 == nullptr)return false;//某一个为空,则不成立
	if (root1->m_nValue != root2->m_nValue)return false;//不相等
	return isSymmetrical(root1->pLeft, root2->pRight)
		&& isSymmetrical(root1->pRight, root2->pLeft);
}
面试题28:对称的二叉树面试题28:对称的二叉树 C_臻可爱呢 发布了85 篇原创文章 · 获赞 14 · 访问量 1174 私信 关注
上一篇:【剑指offer】树的子结构(JS实现)


下一篇:剑指offer 树的子结构