leetcode刷题记录

429. N 叉树的层序遍历

中等

412

相关企业

给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。

树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。

示例 1:

输入:root = [1,null,3,2,4,null,5,6]
输出:[[1],[3,2,4],[5,6]]

示例 2:

输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]

提示:

  • 树的高度不会超过 1000
  • 树的节点总数在 [0, 10^4] 之间

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     int numChildren;
 *     struct Node** children;
 * };
 */

/**
 * 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 Node* root, int* returnSize, int** returnColumnSizes) {
    *returnSize=0;
    if(root==NULL) return NULL;
    int **res=(int**)malloc(sizeof(int *)*1000);
    *returnColumnSizes=(int*)malloc(sizeof(int)*1000);
    int front=0;
    int rear=0;
    int index;
    struct Node *tree[1000];
    tree[rear++]=root;
    while(front<rear){
        index=0;
        int size=rear-front;
        for(int i=0;i<size;++i){
            struct Node *temp=tree[front++];
            res[*returnSize][i]=temp->val;
            for(int j=0;j<temp->numChildren;++j){
                tree[rear++]=(temp->children)[j];
            }

        }
       // returnSize=(int*)malloc(sizeof(int)*size);
        (*returnColumnSizes)[*returnSize]=size;
        (*returnSize)++;
    }
    return res;
}

第一遍代码

思路:构造个队列,用于存放每层遍历过后节点的children,当一层遍历完之后,进入下一层;

但是第一次leetcode编译器报错:

Line 36: Char 32: runtime error: store to misaligned address 0xbebebebebebebebe for type 'int', which requires 4 byte alignment [solution.c] 0xbebebebebebebebe: note: pointer points here <memory cannot be printed>

本小白第一次碰到这个问题,QAQ

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     int numChildren;
 *     struct Node** children;
 * };
 */

/**
 * 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 Node* root, int* returnSize, int** returnColumnSizes) {
    *returnSize=0;
    if(root==NULL) return NULL;
    int **res=(int**)malloc(sizeof(int *)*1000);
    *returnColumnSizes=(int*)malloc(sizeof(int)*1000);
    int front=0;
    int rear=0;
    struct Node *tree[1000];
    tree[rear++]=root;
    while(front<rear){
        int size=rear-front;
        res[*returnSize] = (int*)malloc(sizeof(int)*size);
        for(int i=0;i<size;++i){
            struct Node *temp=tree[front++];
            res[*returnSize][i]=temp->val;
            for(int j=0;j<temp->numChildren;++j){
                tree[rear++]=(temp->children)[j];
            }

        }
        (*returnColumnSizes)[*returnSize]=size;
        (*returnSize)++;
    }
    return res;
}

第二次修改:删掉了一些没用的变量,发现对于每一层在存储数据时没有申请返回数组的空间(==),加了之后又有了新的错误提示:

================================================================= ==20==ERROR: AddressSanitizer: attempting double-free on 0x621000003d00 in thread T0: #0 0x7f95ca063537 in __interceptor_free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:127 #2 0x7f95c9655082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x621000003d00 is located 0 bytes inside of 4000-byte region [0x621000003d00,0x621000004ca0) freed by thread T0 here: #0 0x7f95ca063537 in __interceptor_free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:127 #2 0x7f95c9655082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) previously allocated by thread T0 here: #0 0x7f95ca063887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145 #3 0x7f95c9655082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) SUMMARY: AddressSanitizer: double-free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:127 in __interceptor_free ==20==ABORTING

第三次修改(参考了一些评论)

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     int numChildren;
 *     struct Node** children;
 * };
 */

/**
 * 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 Node* root, int* returnSize, int** returnColumnSizes) {
    *returnSize=0;
    int **res=(int**)malloc(sizeof(int *)*10000);
    *returnColumnSizes=(int*)malloc(sizeof(int)*10000);
    if(root==NULL) return res;


    int front=0;
    int rear=0;
    struct Node *tree[10000];
    tree[rear++]=root;
    while(front<rear){
        int size=rear-front;
        res[*returnSize] = (int*)malloc(sizeof(int)*size);
        for(int i=0;i<size;++i){
            struct Node *temp=tree[front++];
            res[*returnSize][i]=temp->val;
            for(int j=0;j<temp->numChildren;++j){
                tree[rear++]=(temp->children)[j];
            }

        }
        (*returnColumnSizes)[*returnSize]=size;
        (*returnSize)++;
    }
    return res;
}

重点在于一开始在判断是否为空要先申请出res和returnColumnSizes的空间;否则leetcode编译器默认没有returnColumnSizes的返回,导致报错(我的理解)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值