Leetcode:https://leetcode-cn.com/classic/problems/symmetric-tree/description/
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
bool Is_same_or_not(TreeNode* p, TreeNode* q)
{
if (!p && !q)
return true;
else if (!p || !q)
return false;
else
return p->val != q->val ? false : Is_same_or_not(p->left, q->right) && Is_same_or_not(p->right, q->left);
}
bool isSymmetric(TreeNode* root)
{
if (root == NULL)
return true;
else
return Is_same_or_not(root->left, root->right);
}