解题思路:首先,将原二叉树进行镜像处理。得到一个镜像二叉树:(镜像二叉树的求解见https://www.cnblogs.com/sbb-first-blog/p/13289799.html)。
其次,对镜像后的二叉树进行比较,如果二者相同则返回true,否则返回false。
终止条件:1、如果镜像和原二叉树有且只有一个为null则判定为false。
2、如果镜像和元二叉树节点数不一致,则返回false.
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * struct TreeNode *left; 6 * struct TreeNode *right; 7 * }; 8 */ 9 /*递归得到二叉树的镜像 10 并将得到的镜像与原二叉树进行比较*/ 11 //二叉树结点对比 12 bool isEqual(struct TreeNode* first, struct TreeNode* sc){ 13 if(first==0 && sc==0) return true; 14 if(first==0 || sc==0) return false; 15 if(first->val!=sc->val) return false; 16 return isEqual(first->right, sc->right) && isEqual(first->left,sc->left); 17 } 18 //二叉树镜像 19 struct TreeNode* mirrotree(struct TreeNode* root) 20 { 21 if(root==0) return NULL; 22 struct TreeNode* Node=(struct TreeNode*)malloc(sizeof(struct TreeNode)); 23 Node->val=root->val; 24 Node->right=mirrotree(root->left); 25 Node->left=mirrotree(root->right); 26 return Node; 27 } 28 bool isSymmetric(struct TreeNode* root){ 29 // struct TreeNode* Node1=TreeClone(root); 30 // struct TreeNode* Node2=mirrotree(Node1); 31 struct TreeNode* Node2=mirrotree(root); 32 return isEqual(root,Node2); 33 }