class Solution
{
public:
bool isduicheng = true;
bool isSymmetric(TreeNode *root)
{
// return root==nullptr||recur(root->left,root->right);
if (root == nullptr)
{
return isduicheng;
}
recur(root->left, root->right);
return isduicheng;
}
void recur(TreeNode *left, TreeNode *right)
{
if (left == nullptr || right == nullptr || left->val != right->val)
{
/* code */
if (left == nullptr && right == nullptr)
{
}
else
{
isduicheng = false;
}
}
if (left != nullptr && right != nullptr)
{
recur(left->left, right->right);
recur(left->right, right->left);
}
}
};
对称树的判定
最新推荐文章于 2024-07-08 22:12:26 发布
这篇文章描述了一个C++类Solution的方法,用于检查给定的二叉树是否对称。isSymmetric函数使用递归策略,通过比较左右子树节点值的相等性来确定树的对称性。
摘要由CSDN通过智能技术生成