首页 > 其他 > 详细

Symmetric Tree

时间:2014-03-09 14:59:48      阅读:352      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 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 };
bubuko.com,布布扣

Symmetric Tree,布布扣,bubuko.com

Symmetric Tree

原文:http://www.cnblogs.com/crane-practice/p/3585983.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!