# leetcode 101. 对称二叉树

    1
/ \
2   2
/ \ / \
3  4 4  3


    1
/ \
2   2
\   \
3    3


class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)return 1;
return judge(root->left,root->right);
}
int judge(TreeNode * root1,TreeNode *root2){
if(!root1&&!root2)return 1;
else if(root1&&root2&&root1->val==root2->val&&judge(root1->left,root2->right)&&judge(root1->right,root2->left))return 1;
else return 0;
}
};

class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)return 1;
queue<TreeNode *>q;
q.push(root);
while(!q.empty()){
queue<TreeNode *>qt;
vector<int>ans;
while(!q.empty()){
TreeNode * temp=q.front();
q.pop();
if(temp->left){
qt.push(temp->left);
ans.push_back(temp->left->val);
}else{
ans.push_back(0);
}
if(temp->right){
qt.push(temp->right);
ans.push_back(temp->right->val);
}else{
ans.push_back(0);
}
}
q=qt;
if(judge(ans)==0)return 0;
}
return 1;
}
int judge(vector<int>ans){
int n=ans.size()-1;
for(int i=0;i<=n;i++,n--){
if(ans[i]!=ans[n])
return 0;
}
return 1;
}
};

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120