您的位置:首页 > 其它

判断二叉树是否为镜像对称

2017-07-20 12:06 495 查看
leetcode 101

思路:将左右两个对称的树元素分前后送入队列,判断时一次取两个进行判断

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

bool isSymmetric(TreeNode* root) {
if(!root) return true;
queue<TreeNode*>q;
TreeNode*l,*r;
q.push(root->right);
q.push(root->left);
while(!q.empty()) {
r = q.front();
q.pop();
l = q.front();
q.pop();
if(!r && !l) continue;
if(r && !l || !r && l) return false;
if(r->val != l->val) return false;
q.push(r->right);
q.push(l->left);
q.push(r->left);
q.push(l->right);
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二叉树 leetcode