标签:binary 检查 节点 node 递归 amp pre als roo
给定一个二叉树,检查它是否是镜像对称的。
考察递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool compare(struct TreeNode*left, struct TreeNode* right){
if(left==NULL && right!=NULL){return false;}
else if(left!=NULL && right==NULL){return false;}
else if(left==NULL && right==NULL){return true;}
else if(left->val != right->val){return false;}
return compare(left->left, right->right)&&compare(left->right, right->left);
}
bool isSymmetric(struct TreeNode* root){
if(root==NULL){
return true;
}
return compare(root->left, root->right);
}
标签:binary 检查 节点 node 递归 amp pre als roo
原文地址:https://www.cnblogs.com/karkinos/p/14542875.html