【leetcode】打印二叉树链表

int count = 0;
void func(int** arr,int* hash,struct TreeNode* node,int row)
{
    row++;
    if (hash[row] == 0) //如果该行列数为0 那证明是当前遍历到的是该行第一个数(空值已经排除)
    {
        count++; 
        int* row_arr = (int*)calloc(1000,sizeof(int)); 
        arr[row] = row_arr;
        row_arr[hash[row]++] = node->val; //赋值完后列数+1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    }
    else if(hash[row] > 0)
    {
        arr[row][hash[row]++] = node->val;
    }
    if (node->left != NULL)
    {
        func(arr,hash,node->left,row);
    }
    if (node->right != NULL)
    {
        func(arr,hash,node->right,row);
    }
}
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    int** arr = (int**)calloc(10000,sizeof(int));
    int* hash = (int*)calloc(10000,sizeof(int)); //用来存放每行的列数
    if (root == NULL)
    {
        *returnSize = NULL;
        return NULL;
    }
    int* row_arr = (int*)calloc(1,sizeof(int));    
    int row = 0; //行数
    count = 0; //定义了全局变量 用来最后返回行数
    row_arr[hash[row]++] = root->val;
    arr[row] = row_arr;    
    count++;    
    if (root->left != NULL)
    {
        func(arr,hash,root->left,row);
    }
    if (root->right != NULL)
    {
        func(arr,hash,root->right,row);
    }
    *returnSize = count;  //行数
    *returnColumnSizes = hash; //每行列数指针
    return arr;
}
原文地址:https://www.cnblogs.com/ganxiang/p/13549894.html