LeetCode | 101. Symmetric Binary Tree
OJ link
- It is difficult to make a judgment in this function. We define another function to
- If both are equal and empty, return true
- One is empty, one is not empty
- If both are not empty, compare the values.
- Then recurse the left of 1, the right of 2, the right of 1, and the left of 2
bool _isSymmetric(struct TreeNode* root1,struct TreeNode* root2)
{
if(root1 == NULL && root2 == NULL)
return true;
if(root1 == NULL || root2 == NULL)
return false;
if(root1->val != root2->val)
return false;
return _isSymmetric(root1->left,root2->right)
&& _isSymmetric(root1->right,root2->left);
}
bool isSymmetric(struct TreeNode* root) {
return _isSymmetric(root->left,root->right);
}