LeetCode102
解法:
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if(root != NULL) que.push(root);
vector<vector<int>> result;
while(!que.empty())
{
int size = que.size();
vector<int> vec;
for(int i = 0;i < size;i++)
{
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if(node->left != NULL) que.push(node->left);
if(node->right != NULL) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
};
LeetCode107
解法:
class Solution {
public:
void order(TreeNode* node,vector<vector<int>>&result,int deepth){
if(node == nullptr) return;
if(result.size() == deepth) result.push_back(vector<int>());
result[deepth].push_back(node->val);
order(node->left,result,deepth+1);
order(node->right,result,deepth+1);
}
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
vector<vector<int>> resulty;
int deepth = 0;
order(root,result,deepth);
int deep = result.size();
for(int i = 0;i < deep ;i++)
{
resulty.push_back(result[deep - i - 1]);
}
return resulty;
}
};
leetcode 199
题目:
解法:
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> result;
if(root != NULL) result.push(root);
vector<int> s;
while(!result.empty())
{
int size = result.size();
for(int i = 0;i < size;i++)
{
TreeNode* node = result.front();
result.pop();
if(node->left) result.push(node->left);
if(node->right) result.push(node->right);
if(i == size - 1) s.push_back(node->val);
}
}
return s;
}
};
leetcode637
解法:
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> que;
if(root!= NULL) que.push(root);
vector<double> result;
while(!que.empty())
{
int size = que.size();
double sum = 0;
for(int i = 0 ; i < size ; i++)
{
TreeNode* sss = que.front();
sum += sss->val;
que.pop();
if(sss->left) que.push(sss->left);
if(sss->right) que.push(sss->right);
}
double ss;
ss = sum/size*1.00000;
result.push_back(ss);
}
return result;
}
};
leetcode429
题目:
解法
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
queue<Node*> que;
vector<vector<int>> result;
if(root != NULL) que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
for(int i = 0 ; i < size ;i++){
Node* node = que.front();
que.pop();
vec.push_back(node->val);
int size1 = node->children.size();
for(int j = 0 ; j < size1 ; j++)
{
if(node->children[j]) que.push(node->children[j]);
}
}
result.push_back(vec);
}
return result;
}
};
leetcode515
题目:
解法:
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
queue<TreeNode*> que;
if(root != NULL) que.push(root);
vector<int> result;
while(!que.empty()){
int size = que.size();
int max = que.front()->val;
for(int i = 0;i < size; i++)
{
TreeNode *node = que.front();
que.pop();
if(node->val > max) max = node->val;
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
result.push_back(max);
}
return result;
}
};
LeetCode116
题目:
解法:
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if(root != NULL) que.push(root);
while(!que.empty())
{
int size = que.size();
for(int i = 0; i < size;i++)
{
Node* node = que.front();
que.pop();
if(i != size - 1) node->next = que.front();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return root;
}
};
LeetCode117
题目:
解法
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if(root != NULL) que.push(root);
while(!que.empty())
{
int size = que.size();
for(int i = 0; i < size;i++)
{
Node* node = que.front();
que.pop();
if(i != size - 1) node->next = que.front();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return root;
}
};
LeetCode104
题目:
层次遍历解法:
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
if(root != NULL) que.push(root);
int deepth = 0;
while(!que.empty()){
int size = que.size();
for(int i = 0; i < size ;i++)
{
TreeNode* node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
deepth++;
}
return deepth;
}
};
深度遍历解法:
class Solution {
public:
void order(TreeNode* cur,int &deep,int depth)
{
if(cur == nullptr) return;
if(deep == depth) deep++;
order(cur->left,deep,depth+1);
order(cur->right,deep,depth+1);
}
int maxDepth(TreeNode* root) {
int deep = 0;
int depth = 0;
order(root,deep,depth);
return deep;
}
};
leetcode111
题目:
解法:
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
int deep;
if(root != NULL) que.push(root);
deep = 0;
while(!que.empty()){
int size = que.size();
int bb = 0;
for(int i = 0;i < size;i++)
{
TreeNode* node = que.front();
if(node->left == nullptr && node->right == nullptr)
{
deep = deep + 1;
bb = 1;
break;
}
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
if(bb == 1)
{
break;
}
deep++;
}
return deep;
}
};
LeetCode226
题目:
解法1:(层序遍历)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
queue<TreeNode*> que;
if(root != NULL) que.push(root);
while(!que.empty())
{
int size = que.size();
for(int i = 0;i < size;i++)
{
TreeNode* node = que.front();
que.pop();
TreeNode* tmp = node ->left;
node -> left = node ->right;
node -> right = tmp;
if(node -> left) que.push(node -> left);
if(node -> right) que.push(node -> right);
}
}
return root;
}
};
解法2:(深度遍历)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return NULL;
TreeNode* node = root;
invertTree(node -> left);
swap(node -> left,node -> right);
invertTree(node -> left);
return node;
}
};
LeetCode101
题目:
解法:(层序遍历)
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<TreeNode*> que1;
queue<TreeNode*> que2;
if(root)
{
que1.push(root);
que2.push(root);
}
while(!que1.empty() && !que2.empty())
{
int size1 = que1.size();
int size2 = que2.size();
if(size1 != size2) return false;
for(int i = 0; i < size1 ;i++)
{
TreeNode* node1 = que1.front();
TreeNode* node2 = que2.front();
if(node1 -> val != node2 ->val) return false;
que1.pop();
que2.pop();
if((node1 ->left && node2 ->right))
{
que1.push(node1 ->left);
que2.push(node2 ->right);
}
else if((!node1 ->left && node2 ->right) ||(node1 ->left && !node2 ->right))
return false;
if((node1 ->right && node2 ->left))
{
que1.push(node1 ->right);
que2.push(node2 ->left);
}
else if((!node1 ->right && node2 ->left) ||(node1 ->right && !node2 ->left))
return false;
}
}
return true;
}
};