/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root)
{
int i,j;
queue<TreeNode*> range_first_tree;
int queue_size;
TreeNode* process_node;
vector<int> each_layer;
vector<int> each_layer_reverse;
bool return_bool=1;
range_first_tree.push(root);
//核心:每层一次性做完 每一层的nulptr也应该入栈
while(!range_first_tree.empty())
{
queue_size=range_first_tree.size();
for(i=0;i<queue_size;i++)
{
process_node=range_first_tree.front();
range_first_tree.pop();
if(process_node!=nullptr) each_layer.push_back(process_node->val);
else each_layer.push_back(-200);
if(process_node!=nullptr)
{
range_first_tree.push(process_node->left);
range_first_tree.push(process_node->right);
}
}
each_layer_reverse=each_layer;
reverse(each_layer_reverse.begin(),each_layer_reverse.end());
std::cout<<"each layer"<<std::endl;
for(i=0;i<each_layer.size();i++)
{
std::cout<<" "<<each_layer[i];
}
std::cout<<std::endl;
std::cout<<"each_layer_reverse "<<std::endl;
for(i=0;i<each_layer_reverse.size();i++)
{
std::cout<<" "<<each_layer_reverse[i];
}
std::cout<<std::endl;
for(i=0;i<each_layer.size();i++)
{
if(each_layer[i]!=each_layer_reverse[i])
{
return_bool=0;
break;
}
}
each_layer.clear();
each_layer_reverse.clear();
if(return_bool==0) break;
}
return return_bool;
}
};