102. 二叉树的层序遍历
107. 二叉树的层序遍历 II
199. 二叉树的右视图
637. 二叉树的层平均值
429. N 叉树的层序遍历
515. 在每个树行中找最大值
116. 填充每个节点的下一个右侧节点指针
117. 填充每个节点的下一个右侧节点指针 II
104. 二叉树的最大深度
111. 二叉树的最小深度
非递归层序遍历
二叉树的非递归层序遍历需要借用队列来实现。
非递归层序遍历即一层一层遍历,遍历完一层再遍历下一层。所以借用队列的FIFO思想,遍历二叉树的第一层时将左右孩子节点入队列,遍历二叉树的第二层时从队列中取第二层的节点并且将第二层的左右孩子节点入队列,依次循环即可完成。
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> que;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
vector<int> vec;
int levelSize = que.size();
while (levelSize--) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left != nullptr)
que.push(node->left);
if (node->right != nullptr)
que.push(node->right);
}
result.push_back(vec);
}
return result;
}
};
递归层序遍历
二叉树的递归层序遍历需要借助一个遍历来控制二叉树的深度。
递归需要确定三个条件:
-
确定函数的返回类型和参数列表
题目要求将遍历结果放入二维数组中,所以参数要有一个二维数组的引用,并且我们需要一个变量来控制二叉树的深度和二维数组的大小
void order(TreeNode* root, vector<vector<int>>& vec, int depth)
-
确定函数的终止条件
当递归到空节点时,也就不需要再递归了
if (root == nullptr) return;
-
确定单次递归的逻辑
if (root == nullptr) return; if (vec.size() == depth) vec.push_back(vector<int>()); vec[depth].push_back(root->val); order(root->left, vec, depth + 1); order(root->right, vec, depth + 1);
完整代码
class Solution {
public:
void order(TreeNode* root, vector<vector<int>>& vec, int depth) {
if (root == nullptr)
return;
if (vec.size() == depth)
vec.push_back(vector<int>());
vec[depth].push_back(root->val);
order(root->left, vec, depth + 1);
order(root->right, vec, depth + 1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
int depth = 0;
order(root, result, depth);
return result;
}
};
102.二叉树的层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> que;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
vector<int> vec;
int levelSize = que.size();
while (levelSize--) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left != nullptr)
que.push(node->left);
if (node->right != nullptr)
que.push(node->right);
}
result.push_back(vec);
}
return result;
}
};
107. 二叉树的层序遍历 II
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> que;
if (root) que.push(root);
while (!que.empty()) {
vector<int> vec;
int levelSize = que.size();
while (levelSize--) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
reverse(result.begin(), result.end());
return result;
}
};
199. 二叉树的右视图
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
queue<TreeNode*> que;
if (root) que.push(root);
while (!que.empty()) {
int tailValue;
int levelSize = que.size();
while (levelSize--) {
TreeNode* node = que.front();
que.pop();
tailValue = node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(tailValue);
}
return result;
}
};
637. 二叉树的层平均值
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> result;
queue<TreeNode*> que;
if (root) que.push(root);
while (!que.empty()) {
double sum = 0;
int levelSize = que.size();
for (int i = 0; i < levelSize; ++i) {
TreeNode* node = que.front();
que.pop();
sum += node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(sum / levelSize);
}
return result;
}
};
429. N 叉树的层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> result;
queue<Node*> que;
if (root) que.push(root);
while (!que.empty()) {
vector<int> vec;
int levelSize = que.size();
while (levelSize--) {
Node* node = que.front();
que.pop();
vec.push_back(node->val);
vector<Node*> childrens = node->children;
for (int i = 0; i < childrens.size(); ++i) {
if (childrens[i]) que.push(childrens[i]);
}
}
result.push_back(vec);
}
return result;
}
};
515. 在每个树行中找最大值
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> result;
queue<TreeNode*> que;
if (root) que.push(root);
while (!que.empty()) {
int levelSize = que.size();
int maxValue = INT_MIN;
while (levelSize--) {
TreeNode* node = que.front();
que.pop();
if (node->val > maxValue) maxValue = node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(maxValue);
}
return result;
}
};
116. 填充每个节点的下一个右侧节点指针
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root) que.push(root);
while (!que.empty()) {
int levelSize = que.size();
while (levelSize--) {
Node* node = que.front();
que.pop();
if (levelSize) node->next = que.front();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return root;
}
};
117. 填充每个节点的下一个右侧节点指针 II
class Solution {
public:
Node* connect(Node* root) {
if (!root) return root;
Node* cur = root;
while (cur) {
Node* dummy = new Node;
Node* prev = dummy;
while (cur) {
if (cur->left) {
prev->next = cur->left;
prev = prev->next;
}
if (cur->right) {
prev->next = cur->right;
prev = prev->next;
}
cur = cur->next;
}
cur = dummy->next;
}
return root;
}
};
104. 二叉树的最大深度
class Solution {
public:
int maxDepth(TreeNode* root) {
int depth = 0;
queue<TreeNode*> que;
if (root) que.push(root);
while (!que.empty()) {
++depth;
int levelSize = que.size();
while (levelSize--) {
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return depth;
}
};
111. 二叉树的最小深度
class Solution {
public:
int minDepth(TreeNode* root) {
int depth = 0;
queue<TreeNode*> que;
if (root) que.push(root);
while (!que.empty()) {
++depth;
int levelSize = que.size();
while (levelSize--) {
TreeNode* node = que.front();
que.pop();
if (!node->left && !node->right) return depth;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return depth;
}
};