给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ ``\
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回锯齿形层次遍历如下:
[
[3],
[20,9],
[15,7]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回其自底向上的层次遍历为:
[
[15,7],
[9,20],
[3]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii
方法一:BFS一层一层搜,先记录队列的个数,便于将一层存入数组的一行
方法二:DFS利用前序遍历,按当前层次插入到对应数组的行数
方法一:其实就是在第一题的基础上加一个判断语句,从第0行开始,偶数行正向输出(后插法),奇数项反向输出(前插法),但是C语言数组不适合前插,可以在末尾对奇数项数组进行反转即可
方法二:用双栈模拟,栈1存偶数项,栈2存奇数项即可
方法三:用双向队列代替栈来模拟,C语言不好实现,用C++
方法一:因为可以知道其深度,在第一题的基础上从后往前存储即可
方法二:层序遍历时将每一行前插C++
方法三:在第一题基础上反转数组的行
方法一:
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int maxDepth(struct TreeNode* root){
if(root==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL){
return 1;
}
else{
int a=maxDepth(root->left);
int b=maxDepth(root->right);
return 1+(a>b?a:b);
}
}
void level(struct TreeNode *root,int **res,int resSize,int **reColSize)
{
if(root==NULL)return;
res[resSize][(*reColSize)[resSize]++]=root->val;
if(root->left)
level(root->left,res,resSize+1,reColSize);
if(root->right)
level(root->right,res,resSize+1,reColSize);
}
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
*returnSize=maxDepth(root);
*returnColumnSizes=(int *)malloc(sizeof(int)*(*returnSize));
int **res=(int **)malloc(sizeof(int *)*(*returnSize));
for(int i=0;i<(*returnSize);i++)
{
(*returnColumnSizes)[i]=0;
res[i]=(int *)malloc(sizeof(int)*1000);
}
level(root,res,0,returnColumnSizes);
return res;
}
方法二:
int maxDepth(struct TreeNode* root){
if(root==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL){
return 1;
}
else{
int a=maxDepth(root->left);
int b=maxDepth(root->right);
return 1+(a>b?a:b);
}
}
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
*returnSize=maxDepth(root);
*returnColumnSizes=(int *)malloc(sizeof(int)*(*returnSize));
int **res=(int **)malloc(sizeof(int *)*(*returnSize));
if(root==NULL)return res;
struct TreeNode *queue[500];
int front=0,rear=0;
int resSize=0;
queue[rear++]=root;
while(rear!=front)
{
int sum=rear-front;
res[resSize]=(int *)malloc(sizeof(int)*sum);
(*returnColumnSizes)[resSize]=sum;
for(int i=0;i<sum;i++)
{
root=queue[(front++)%500];
res[resSize][i]=root->val;
if(root->left)
queue[(rear++)%500]=root->left;
if(root->right)
queue[(rear++)%500]=root->right;
}
resSize++;
}
return res;
}
方法一:
int maxDepth(struct TreeNode* root){
if(root==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL){
return 1;
}
else{
int a=maxDepth(root->left);
int b=maxDepth(root->right);
return 1+(a>b?a:b);
}
}
void level(struct TreeNode *root,int **res,int resSize,int **reColSize)
{
if(root==NULL)return;
res[resSize][(*reColSize)[resSize]++]=root->val;
if(root->left)
level(root->left,res,resSize+1,reColSize);
if(root->right)
level(root->right,res,resSize+1,reColSize);
}
int** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
*returnSize=maxDepth(root);
*returnColumnSizes=(int *)malloc(sizeof(int)*(*returnSize));
int **res=(int **)malloc(sizeof(int *)*(*returnSize));
for(int i=0;i<(*returnSize);i++)
{
(*returnColumnSizes)[i]=0;
res[i]=(int *)malloc(sizeof(int)*1000);
}
level(root,res,0,returnColumnSizes);
for(int i=1;i<*returnSize;i=i+2)
{
int len=(*returnColumnSizes)[i];
for(int j=0;j<len/2;j++)
{
int temp=res[i][j];
res[i][j]=res[i][len-j-1];
res[i][len-j-1]=temp;
}
}
return res;
}
方法二:
maxDepth(struct TreeNode *root)
{
if(root==NULL)return 0;
return fmax(maxDepth(root->left),maxDepth(root->right))+1;
}
int** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
int depth=maxDepth(root);
int **res=(int **)malloc(sizeof(int *)*depth);
*returnSize=depth;
if(root==NULL){return res;}
*returnColumnSizes=(int *)malloc(sizeof(int *)*depth);
struct TreeNode *stack1[500];
struct TreeNode *stack2[500];
int top1=0;
int top2=0;
stack1[top1++]=root;
int k=0;
while(top1!=0||top2!=0)
{
if(k%2==0)
{
int i=0;
res[k]=(int *)malloc(sizeof(int)*top1);//printf("%d ",top1);
while(top1!=0)
{
root=stack1[top1-1];
//printf("%d ",root->val);
res[k][i++]=root->val;
if(root->left!=NULL)
stack2[top2++]=root->left;
if(root->right!=NULL)
stack2[top2++]=root->right;
top1--;
}
(*returnColumnSizes)[k]=i;
k++;
}
else
{
int i=0;
res[k]=(int *)malloc(sizeof(int)*top2);//printf("%d ",top2);
while(top2!=0)
{
root=stack2[top2-1];
//printf("%d ",root->val);
res[k][i++]=root->val;
if(root->right!=NULL)
stack1[top1++]=root->right;
if(root->left!=NULL)
stack1[top1++]=root->left;
top2--;
}
(*returnColumnSizes)[k]=i;
k++;
}
}
return res;
}
方法三:
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL) return res;
deque<TreeNode*> mQueue;
mQueue.push_back(root);
int len = 0;
bool flash = true; // 默认先进后出,false 后进先出
while(!mQueue.empty()) {
len = mQueue.size();
vector<int> tRes;
while(len > 0) {
TreeNode *front ;
if(flash) {
front = mQueue.front();
mQueue.pop_front();
}else{
front = mQueue.back();
mQueue.pop_back();
}
tRes.push_back(front->val);
len--;
if(flash) {
if(front->left) mQueue.push_back(front->left);
if(front->right) mQueue.push_back(front->right);
}else{// 后进的情况下,需要先加入 front->right 指针,以保证队列中顺序是不变的
if(front->right) mQueue.push_front(front->right);
if(front->left) mQueue.push_front(front->left);
}
}
res.push_back(tRes);
flash = !flash;
}
return res;
}
};
int maxDepth(struct TreeNode* root){
if(root==NULL)
return 0;
else if(root->left==NULL&&root->right==NULL){
return 1;
}
else{
int a=maxDepth(root->left);
int b=maxDepth(root->right);
return 1+(a>b?a:b);
}
}
void level(struct TreeNode *root,int **res,int resSize,int **reColSize)
{
if(root==NULL)return;
res[resSize][(*reColSize)[resSize]++]=root->val;
if(root->left)
level(root->left,res,resSize-1,reColSize);
if(root->right)
level(root->right,res,resSize-1,reColSize);
}
int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
*returnSize=maxDepth(root);
*returnColumnSizes=(int *)malloc(sizeof(int)*(*returnSize));
int **res=(int **)malloc(sizeof(int *)*(*returnSize));
for(int i=0;i<(*returnSize);i++)
{
(*returnColumnSizes)[i]=0;
res[i]=(int *)malloc(sizeof(int)*1000);
}
level(root,res,*returnSize-1,returnColumnSizes);
return res;
}
方法二:
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> temp;//临时变量
int len = q.size();//队列的长度(队列中树的个数)
for(int i=0;i<len;i++)
{
TreeNode* now = q.front();//获取队列的第一个
q.pop();//队列弹出
temp.push_back(now->val);//临时变量保存第一颗树的根节点
if(now->left!=NULL) //左子树非空
q.push(now->left);
if(now->right!=NULL)//右子树非空
q.push(now->right);
}
res.insert(res.begin(), temp);//将临时变量保存到结果中(从前面插入)
}
return res;
}
};
方法三:
class Solution {
List<List<Integer>> levels = new ArrayList<List<Integer>>();
public void helper(TreeNode node, int level) {
// start the current level
if (levels.size() == level)
levels.add(new ArrayList<Integer>());
// fulfil the current level
levels.get(level).add(node.val);
// process child nodes for the next level
if (node.left != null)
helper(node.left, level + 1);
if (node.right != null)
helper(node.right, level + 1);
}
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if (root == null) return levels;
helper(root, 0);
Collections.reverse(levels);//将集合中的所有对象按倒序排列
return levels;
}
}