forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2066 from andrewmustea/add_0589-n-ary-…
…tree-preorder-traversal.c create 0589-n-ary-tree-preorder-traversal.c
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Given the root of an n-ary tree, return the preorder traversal of its nodes' | ||
* values. | ||
* | ||
* Nary-Tree input serialization is represented in their level order traversal. | ||
* Each group of children is separated by the null value (See examples) | ||
* | ||
* Constraints: | ||
* | ||
* The number of nodes in the tree is in the range [0, 104]. | ||
* 0 <= Node.val <= 10^4 | ||
* The height of the n-ary tree is less than or equal to 1000. | ||
* | ||
* Definition for a Node. | ||
* struct Node { | ||
* int val; | ||
* int numChildren; | ||
* struct Node** children; | ||
* }; | ||
* | ||
* Note: The returned array must be malloced, assume caller calls free(). | ||
* | ||
* Space: O(n) | ||
* Time: O(n) | ||
*/ | ||
|
||
void traverse(struct Node* root, int *ret, int* index) { | ||
if (!root) | ||
return; | ||
|
||
ret[(*index)++] = root->val; | ||
|
||
for (int i = 0; i < root->numChildren; ++i) | ||
traverse(root->children[i], ret, index); | ||
} | ||
|
||
int* preorder(struct Node* root, int* returnSize) { | ||
int *ret = malloc(10000 * sizeof(int)); | ||
*returnSize = 0; | ||
traverse(root, ret, returnSize); | ||
return ret; | ||
} |