Share my solution in C
//// iterative solution\n\n    int preorderTraversal(struct TreeNode root, int returnSize) {\n    int result = NULL;\n    if (root == NULL)\n        return resu
//// iterative solution\n\n    int* preorderTraversal(struct TreeNode* root, int* returnSize) {\n    int *result = NULL;\n    if (root == NULL)\n        return result;\n        \n    *returnSize = 0;\n    \n    struct TreeNode **stack = (struct TreeNode **)malloc(sizeof(struct TreeNode *));\n    struct TreeNode *pop;\n    int length = 0;\n    stack[length++] = root;\n    \n    while (length > 0) {\n        result = (int *)realloc(result, (*returnSize+1)*sizeof(int));\n        pop = stack[--length];\n        result[*returnSize] = pop->val;\n        *returnSize += 1;\n        if (pop->right) {\n            stack = (struct TreeNode **)realloc(stack, sizeof(struct TreeNode*)*(length+1));\n            stack[length++] = pop->right;\n        }\n        if (pop->left) {\n            stack = (struct TreeNode **)realloc(stack, sizeof(struct TreeNode*)*(length+1));\n            stack[length++] = pop->left;\n        }\n    }\n    free(stack);\n    return result;\n}\n\n//// recursive solution\n\n    int* preorderTraversal(struct TreeNode* root, int* returnSize) {\n    int *result = NULL;\n    if (root == NULL)\n        return result;\n    result = (int *)malloc(sizeof(int));\n    *result = root->val;\n    \n    int leftsize=0, rightsize=0, *leftarr, *rightarr;\n    if (root->left)\n        leftarr = preorderTraversal(root->left, &leftsize);\n    if (root->right)\n        rightarr = preorderTraversal(root->right, &rightsize);\n    \n    *returnSize = 1 + leftsize + rightsize;\n    if (leftsize >0 || rightsize > 0)\n        result = (int *)realloc(result, sizeof(int)*(*returnSize));\n    \n    int i, j;\n    for (i=0; i<leftsize; i++)\n        result[i+1] = leftarr[i];\n    if (leftsize > 0)\n        free(leftarr);\n    for (j=0; j<rightsize; j++)\n        result[i+j+1] = rightarr[j];\n    if (rightsize > 0)\n        free(rightarr);\n        \n    return result;\n}
[]