deque支持随机读取,同时又有queue的特性,所以只要使用一个即可
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot) {
if(!pRoot) return true;
deque<TreeNode*> d;
d.push_back(pRoot);
while(!d.empty()){
int n = d.size();
for(int i = 0; i<n; i++){
TreeNode* cur = d.front();
d.pop_front();
if(!cur) continue;
if(cur->left) d.push_back(cur->left);
else d.push_back(nullptr);
if(cur->right) d.push_back(cur->right);
else d.push_back(nullptr);
}
n = d.size();
if(n%2) return false;
for(int i = 0; i<n/2; i++){
if(!d[i] && !d[n-1-i]) continue;
else if(!d[i] || !d[n-1-i] || d[i]->val != d[n-1-i]->val) return false;
}
}
return true;
}
};