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 isSymmetricSame(TreeNode *p,TreeNode *q)
13     {
14         if(!p&&!q)
15         {
16             return true;
17         }
18         else if((!p&&q)||(p&&!q)||(p->val!=q->val))
19         {
20             return false;
21         }
22         else
23         {
24             return isSymmetricSame(p->left,q->right)&&isSymmetricSame(p->right,q->left);
25         }
26     }
27     
28     bool isSymmetric(TreeNode *root) {
29         if(!root)
30         {
31             return true;
32         }
33         else
34         {
35             /*TreeNode *p,*q;
36             p=root->left;
37             q=root->right;*/
38             return isSymmetricSame(root->left,root->right);
39             
40         }
41     }
42 };

 

posted on 2014-03-07 11:04  crane_practice  阅读(106)  评论(0编辑  收藏  举报

导航