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;
}
上一篇:【leetcode】48. 全排列 2


下一篇:【leetcode】18. 四数之和