题目汇总
力扣429:N叉树的层序遍历
题目:
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)
树的序列化输入是用层序遍历,每组子节点都由 null 值分隔。(参见示例)
解题:
使用迭代的方式遍历:
/*
// 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(nullptr == root) return res;
queue<pair<const Node*,int>> q; // 创建队列
q.push({root,0});
while(!q.empty()){
auto [cur,layer] = q.front(); // 记录队首
q.pop(); // 出队
if(res.size() == layer) res.push_back({});
res[layer].push_back(cur->val);
for(auto child:cur->children){
q.push({child,layer+1});
}
}
return res;
}
};
使用递归的方式遍历:
class Solution {
public:
void levelOrder(Node* root,int layer,vector<vector<int>>& res){
if(nullptr == root) return;
if(res.size() == layer) res.push_back({});
res[layer].push_back(root->val);
for(auto child:root->children){
levelOrder(child,layer+1,res);
}
}
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
if(nullptr == root) return res;
levelOrder(root,0,res);
return res;
}
};
另一种使用递归的方式遍历:
class Solution {
public:
int depth(const Node* root){
if(nullptr == root) return 0;
int maxdepth = 0;
for(auto child:root->children){
maxdepth = max(maxdepth,depth(child));
}
return maxdepth+1;
}
// 打印当前层的节点
void level(const Node* root,int n,vector<int>& vec){
if(n<0) return;
if(n == 0){
vec.push_back(root->val);
return;
}
for(auto child:root->children){
level(child,n-1,vec);
}
}
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
if(nullptr == root) return res;
int n = depth(root);
for(int i=0;i<n;++i){
vector<int> vec;
level(root,i,vec);
res.push_back(vec);
}
return res;
}
};
剑指 Offer 32 - I. 从上到下打印二叉树
题目:
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
解题:
迭代的方法
/**
* 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<int> levelOrder(TreeNode* root) {
vector<int> res; // 创建需要返回的数组
if(nullptr == root) return res;
queue<TreeNode*> q; // 创建队列用于遍历
q.push(root); // 把根节点放入
while(!q.empty()){
TreeNode* cur = q.front(); // cur用来记录数据
q.pop();
res.push_back(cur->val); // 把数据放在res中
// 如果有左右子节点,把子节点放在队列末尾,等待遍历
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
return res;
}
};
剑指 Offer 32 - II. 从上到下打印二叉树 II
题目:
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
解题:
/**
* 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; // 创建用来返回的二维数组
queue<TreeNode*> q; // 创建队列,用来放准备遍历的数组
if(nullptr == root) return res;
q.push(root); // 放入头节点
while(!q.empty()){
vector<int> temp; // 创建数组,放每一行的数字
for(int i=q.size();i>0;--i){ // 只pop当前长度次
TreeNode* cur = q.front();
q.pop();
temp.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
res.push_back(temp); // 把攒下的一行放在res中
}
return res;
}
};
注意: 一定要写成
for(int i=q.size();i>0;--i)
而不能写成for(int i=0;i<q.size();++i)
,因为每次循环回来 q.size() 的值是会变的
剑指 Offer 32 - III. 从上到下打印二叉树 III
题目:
请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。
解题:
奇偶层分情况翻转temp,完成题目要求
/**
* 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;
queue<TreeNode*> q;
if(nullptr == root) return res;
q.push(root);
int count = 0;
while(!q.empty()){
vector<int> temp;
for(int i=q.size();i>0;--i){
TreeNode* cur = q.front();
q.pop();
temp.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
// 奇数层不翻转temp,偶数层翻转
if(count++%2 == 1){
reverse(temp.begin(),temp.end());
}
res.push_back(temp);
}
return res;
}
};