测评
题目
代码
int* preorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize = 0;
if (root == NULL) return 0;
#define MAX 1000000
int *arr = (int *)malloc(sizeof(int) * MAX);
struct TreeNode *stack[MAX], *p;
int top = -1;
p = root;
while (top >= 0 || p) {
if (p) {
arr[(*returnSize)++] = p->val;
stack[++top] = p;
p = p->left;
} else {
p = stack[top--];
p = p->right;
}
}
return arr;
}