class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>>result;
queue<TreeNode*>que;
if(root!=NULL)
que.push(root);
while(!que.empty())
{
int size=que.size();
vector<int>temp;
for(int i=0;i<size;i++)
{
TreeNode*node=que.front();
que.pop();
temp.push_back(node->val);
if(node->left)
que.push(node->left);
if(node->right)
que.push(node->right);
}
result.push_back(temp);
}
return result;
}
};
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL)
return root;
swap(root->left,root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
递归的逻辑比较巧妙,一开始脑子没转过来
class Solution {
public:
bool compare(TreeNode*left,TreeNode*right)
{
if(left==NULL&&right!=NULL)
return false;
else if(left!=NULL&&right==NULL)
return false;
else if(left==NULL&&right==NULL)
return true;
else if(left->val!=right->val)
return false;
bool out=compare(left->left, right->right);
bool in=compare(left->right, right->left);
bool issame=out&∈
return issame;
}
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true;
return compare(root->left,root->right);
}
};