标签:树、深度优先搜索、广度优先搜索
• 题号:101
• 题目:对称二叉树
• 难度:简单
• https://leetcode-cn.com/problems/symmetric-tree/
给定一个二叉树,检查它是否是镜像对称的。
/**
* 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) return true;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> v(size);
for(int i = 0; i<size; ++i){
root = q.front(); q.pop();
v[i] = root ? root->val : INT_MIN;
if(root) { q.push(root->left); q.push(root->right); }
}
for(int i = 0; i< size/2; ++i){
if(v[i] != v[size-1-i]) return false;
}
}
return true;
}
};