102.二叉树的层次遍历

题目

给你二叉树的根节点 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;
}

 

时间空间复杂度

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值