给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
代码实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool check(struct TreeNode* l_root,struct TreeNode* r_root)
{
if (l_root == NULL && r_root == NULL)
return true;
else if (l_root == NULL || r_root ==NULL)
return false;
else if(l_root->val != r_root->val)
return false;
return (check(l_root->left, r_root->right));
}
bool isSymmetric(struct TreeNode* root){
if (root == NULL)
return true;
return(check(root->left,root->right));
}