其他分享
首页 > 其他分享> > Leetcode 589. N叉树的前序遍历(DAY 3)

Leetcode 589. N叉树的前序遍历(DAY 3)

作者:互联网

原题题目

在这里插入图片描述




代码实现

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

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

#define MAX 11000

void visit(struct Node* root,int* returnSize,int* Array)
{
    if(!root)
        return;
    Array[(*returnSize)++] = root->val;
    int i;
    for(i=0;i<root->numChildren;i++)
        visit(root->children[i],returnSize,Array);
    return;
}

int* preorder(struct Node* root, int* returnSize) {
    int *Array = (int*)malloc(sizeof(int) * MAX);
    *returnSize = 0;
    visit(root,returnSize,Array);
    return Array;
}

标签:Node,struct,returnSize,int,前序,Leetcode,Array,root,DAY
来源: https://blog.csdn.net/qq_37500516/article/details/111472757