Preorder Traversal | C solution
IntuitionApproachComplexity
Time complexity: O(n)

Space complexity: O(n)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
![Screenshot 2025-01-26 152536.png](https://assets.leetcode.com/users/images/65b6b729-b45a-44d8-988c-8960336c4620_1737885352.6659842.png)

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```c []
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void preOrder(struct TreeNode* root, int *arr, int *returnSize){
    if(root!=NULL){
        arr[(*returnSize)++]=root->val;
        preOrder(root->left, arr, returnSize);
        preOrder(root->right, arr, returnSize);
    }
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int* arr=(int*)malloc(sizeof(int)*100);
    *returnSize=0;
    preOrder(root, arr, returnSize);
    return arr;
    free(arr);
}
```
['C']