用bfs层次遍历,队列实现
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
const int MIN=-0x3f3f3f3f;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int length = q.size();
vector<int> vis(length);
for(int i = 0; i<length; ++i)
{
root = q.front();
q.pop();
vis[i] = (root!=NULL ? root->val : MIN);
if(root) { q.push(root->left); q.push(root->right); }
}
for(int i = 0; i< length/2; ++i)
{
if(vis[i] != vis[length-1-i])
return false;
}
}
return true;
}
};
原文:https://www.cnblogs.com/Hunter01/p/12595356.html