题目
给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例
思路
层次遍历按层输出树中数据
先访问根节点->入队->出队时访问是否存在子节点,存在入队,将所以子节点入队之后,出队的同时访问是否存在子节点,存在入队
一层出对之后,再对下一层出队
代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 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** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
*returnSize = 0;
if(root == NULL)
{
return NULL;
}
int ** ans = (int **)malloc(sizeof(int *) * 2001);
*returnColumnSizes = (int *)malloc(sizeof(int) * 2001);
struct TreeNode * queueData[2001];
int queueFront = 0;
int queueRear = 0;
struct TreeNode * cur;
queueData[queueRear++] = root;
while(queueRear != queueFront)
{
int dataLen = 0;
int last = queueRear;
ans[*returnSize] = (int*)malloc(sizeof(int) * (last - queueFront));
while(queueFront < last)
{
cur = queueData[queueFront++];
ans[*returnSize][dataLen++] = cur ->val;
if(cur -> left != NULL)
queueData[queueRear++] = cur -> left;
if(cur -> right != NULL)
queueData[queueRear++] = cur -> right;
}
(*returnColumnSizes)[*returnSize] = dataLen;
(*returnSize)++;
}
return ans;
}
上述代码存在一个问题,只能解决有限个数据,如果数据反复增大,将反复修改内容,所以对其进行优化,将队列改为链式队列,来一个数据就再加一个节点
struct Queue
{
struct TreeNode* node;
struct Queue* next;
};
void enQueue(struct Queue *queueHead, struct TreeNode* node)
{
//插入到尾部
struct Queue* queueIt = queueHead;
while(queueIt->next != NULL)
{
queueIt = queueIt->next;
}
struct Queue *queueNew = malloc(sizeof(struct Queue));
queueNew->node = node;
queueNew->next = NULL;
queueIt->next = queueNew;
}
struct TreeNode* deQueue(struct Queue *queueHead)
{
//插入到尾部
struct Queue* queueIt = queueHead->next;
struct TreeNode* node;
if(queueIt != NULL)
{
queueHead->next = queueIt->next;
node = queueIt->node;
free(queueIt); //释放空间
return node;
}
return NULL;
}
void freeQueue(struct Queue *queueHead)
{
//清理链表
struct Queue* queueIt = queueHead->next;
struct Queue* queuePre = queueHead->next;
if(queueIt != NULL)
{
queuePre = queueIt;
queueIt = queueIt->next;
free(queuePre); //释放空间
}
free(queueHead);
}
void bfs(int* returnSize,int** returnColumnSizes,int **returnNum,struct Queue* queueHead)
{
struct Queue* queueIt = queueHead->next;
if (queueIt->node == NULL)
{
return;
}
int count = 0;
returnNum[*returnSize] = (int*)malloc(sizeof(int) * 2000);
while(1)
{
struct TreeNode * node = deQueue(queueHead);
if(node == NULL)
{
break;
}
returnNum[*returnSize][count] = node->val;
count++;
if(node->left != NULL)
{
enQueue(queueHead,node->left);
}
if(node->right != NULL)
{
enQueue(queueHead,node->right);
}
}
enQueue(queueHead,NULL);
(*returnColumnSizes)[*returnSize ] = count;
*returnSize = *returnSize + 1;
bfs(returnSize,returnColumnSizes,returnNum,queueHead);
}
int** levelOrder(struct TreeNode* root,int* returnSize,int** returnColumnSizes)
{
*returnSize = 0;
if(root == NULL)
return NULL;
int** returnNum = (int**)malloc(sizeof(int*) * 2000);
*returnColumnSizes = (int*)malloc(sizeof(int) * 2000);
struct Queue *queueHead = malloc(sizeof(struct Queue));
queueHead->next = NULL;
queueHead->node = NULL;
enQueue(queueHead,root);
enQueue(queueHead,NULL);
bfs(returnSize,returnColumnSizes,returnNum,queueHead);
freeQueue(queueHead);
return returnNum;
}
时间空间复杂度