Symmetric Tree

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isM(TreeNode *p,TreeNode *q)
13     {
14         if((p->left!=NULL)^(q->right!=NULL))
15             return false;
16         if((p->right!=NULL)^(q->left!=NULL))
17             return false;
18         if(p->val!=q->val)
19             return false;
20         if(p->left!=NULL&&(isM(p->left,q->right)==false))
21             return false;
22         if((p->right!=NULL)&&(isM(p->right,q->left)==false))
23             return false;
24         return true;
25     }
26     bool isSymmetric(TreeNode *root) {
27         // Start typing your C/C++ solution below
28         // DO NOT write int main() function
29         if(root==NULL)
30             return true;
31         if(!(root->left||root->right))
32             return true;
33         if((root->left!=NULL)&&(root->right!=NULL))
34         {
35             return isM(root->left,root->right);
36         }
37         return false;
38     }
39 };
原文地址:https://www.cnblogs.com/mengqingzhong/p/3073108.html