题目
代码
int* postorderTraversal(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 = root, *pre = NULL;//pre记录节点是从左边返回还是右边
int top = -1;
while (top >= 0 || p) {
if (p) {
stack[++top] = p;
p = p->left;
} else {
p = stack[top];
if (p->right != pre && p->right) p = p->right;
else {
arr[(*returnSize)++] = p->val;
top--;
pre = p;
p = NULL;
}
}
}
return arr;
}