1 /**
 2  * Definition for a binary tree node.
 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 static int wing=[]()
11 {
12     std::ios::sync_with_stdio(false);
13     cin.tie(NULL);
14     return 0;
15 }();
16 
17 class Solution 
18 {
19 public:
20     bool isSymmetric(TreeNode* root) 
21     {
22         if(root==NULL)
23             return true;
24         return IsMirror(root->left,root->right);
25     }
26     
27     bool IsMirror(TreeNode* p,TreeNode* q)
28     {
29         if(p==NULL&&q==NULL)
30             return true;
31         if(p==NULL||q==NULL)
32             return false;
33         if(p->val!=q->val)
34             return false;
35         return IsMirror(p->left,q->right)&&IsMirror(p->right,q->left);
36     }
37 };

用个镜像函数来递归判定,大树化小树,经典递归

posted on 2018-04-17 14:54  高数考了59  阅读(114)  评论(0)    收藏  举报