题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
递归
class Solution {
public:
bool isSymmetrical2(TreeNode* l, TreeNode* r) {
if ((l != nullptr) ^ (r != nullptr)) return false;
if (!l && !r) return true;
return l->val == r->val && isSymmetrical2(l->left, r->right) && isSymmetrical2(l->right, r->left);
}
bool isSymmetrical(TreeNode* pRoot) {
if (!pRoot) return true;
return isSymmetrical2(pRoot->left, pRoot->right);
}
};
