/**
* 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:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
vector<int> hang;
queue<TreeNode*> temp;
temp.push(root);
if(root == NULL)
{
return res;
}
while(!temp.empty())
{
hang.resize(0);
int size = temp.size();
for(int i = 0 ; i < size ; i++)
{
root = temp.front();
hang.push_back(root -> val);
temp.pop();
if(root-> left != NULL) temp.push(root -> left);
if(root-> right != NULL) temp.push(root -> right);
}
res.push_back(hang);
}
return res;
}
};
226. 翻转二叉树
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return root;
swap(root->left , root -> right );
invertTree(root -> right);
invertTree(root -> left);
return root;
}
};
其实这道题虽然用的是递归,但是只有第一次迭代的返回值有效。
递归
/**
* 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 myfun(TreeNode* le, TreeNode* ri)
{
if(le == NULL && ri!= NULL) return 0;
else if(le != NULL && ri == NULL) return 0;
else if(le != NULL && ri != NULL)
{
if(le -> val != ri -> val) return 0;
return myfun(le->left, ri -> right)&&myfun(le->right , ri->left);
}
else return 1;
}
bool isSymmetric(TreeNode* root) {
int res = 1;
if(root == NULL) return res;
res = myfun(root -> left , root -> right );
return res;
}
};
迭代
/**
* 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) {
queue <TreeNode*> temp;
TreeNode* leftcode;
TreeNode* rightcode;
if(root == NULL) return 1;
temp.push(root -> left);
temp.push(root -> right);
while(!temp.empty())
{
leftcode = temp.front() ;
temp.pop();
rightcode = temp.front();
temp.pop();
if(leftcode == NULL && rightcode != NULL) return 0;
else if(leftcode != NULL && rightcode == NULL) return 0;
else if(leftcode != NULL && rightcode != NULL)
{
if(leftcode -> val != rightcode -> val) return 0;
temp.push(leftcode -> left);
temp.push(rightcode -> right);
temp.push(leftcode -> right);
temp.push(rightcode -> left);
}
}
return 1;
}
};
/**
* 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 isSameTree(TreeNode* p, TreeNode* q) {
if(p == NULL && q!= NULL ) return 0;
else if(p != NULL && q == NULL )return 0;
else if(p != NULL && q != NULL)
{
if(p -> val != q -> val )return 0;
return isSameTree(p->left , q-> left)&&isSameTree(p-> right , q->right);
}
else return 1;
}
};