【题目描述】
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3【思路】
题目还是挺简单的,就不赘述了。
【代码】
recursive solution:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
TreeNode* leftnode=root->left;
TreeNode* rightnode=root->right;
return judge(leftnode,rightnode);
}
bool judge(TreeNode* p,TreeNode* q){
if(p==NULL&&q==NULL) return true;
else if(p==NULL||q==NULL) return false;
if(p->val!=q->val) return false;
else return judge(p->left,q->right)&&judge(p->right,q->left);
}
};