团灭leetcode 树的层次遍历问题,层次遍历本质就是BFS广度优先搜索,在前文https://blog.csdn.net/weixin_43976833/article/details/105740752的基础上稍加改动即可完成。
leetcode 102. 二叉树的层序遍历
题目:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
vector<int> curr_level;
int nums = q.size();
for(int i = 0; i < nums; i++) {
TreeNode* curr_node = q.front();
q.pop();
curr_level.push_back(curr_node->val);
if(curr_node->left != NULL)
q.push(curr_node->left);
if(curr_node->right != NULL)
q.push(curr_node->right);
}
res.push_back(curr_level);
}
return res;
}
};
leetcode 107. 二叉树的层序遍历2
题目:https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
核心:在lc 102的基础上,改用 insert 从头插入
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
vector<int> curr_level;
int nums = q.size();
for(int i = 0; i < nums; i++) {
TreeNode* curr_node = q.front();
q.pop();
curr_level.push_back(curr_node->val);
if(curr_node->left != NULL)
q.push(curr_node->left);
if(curr_node->right != NULL)
q.push(curr_node->right);
}
res.insert(res.begin(), curr_level);
}
return res;
}
};
leetcode 103. 二叉树的锯齿形层次遍历
题目:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
核心:在lc 102的基础上,(1) 用 insert 从头插入; (2) 使用标志位.
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL) return res;
queue<TreeNode*> q;
q.push(root);
bool inverse = false;
while(!q.empty()) {
vector<int> curr_level;
int nums = q.size();
for(int i = 0; i < nums; i++) {
TreeNode* curr_node = q.front();
q.pop();
if(!inverse)
curr_level.push_back(curr_node->val);
else if(inverse)
curr_level.insert(curr_level.begin(), curr_node->val);
if(curr_node->left != NULL)
q.push(curr_node->left);
if(curr_node->right != NULL)
q.push(curr_node->right);
}
inverse = !inverse;
res.push_back(curr_level);
}
return res;
}
};
leetcode 429. N叉树的层序遍历
题目:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
核心:本质与lc 102相同
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
if(root == NULL) return res;
queue<Node*> q;
q.push(root);
while(!q.empty()) {
vector<int> curr_level;
int nums = q.size();
for(int i = 0; i < nums; i++) {
Node* curr_node = q.front();
q.pop();
curr_level.push_back(curr_node->val);
for(int i = 0; i < curr_node->children.size(); i++) {
q.push(curr_node->children[i]);
}
}
res.push_back(curr_level);
}
return res;
}
};