void preorder(struct TreeNode*root, int *ret, int *size){
if(root == NULL)
return;
ret[(*size)++] = root->val;
preorder(root->left, ret, size);
preorder(root->right, ret, size);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize = 0;
int *ret = malloc(sizeof(int)*2000);
preorder(root, ret, returnSize);
return ret;
}
自己写了个栈
struct TreeNode;
struct Stack{
struct TreeNode **stk;
int top;
};
struct Stack *CreateStack(int x){
struct Stack *create = malloc(sizeof(struct Stack));
create->stk = malloc(sizeof(struct TreeNode*)*x);
create->top = 0;
return create;
}
void Push(struct Stack *s, struct TreeNode *x){
s->stk[s->top++] = x;
}
struct Stack *Pop(struct Stack *s){
int t = s->top-1;
s->top--;
return s->stk[t];
}
bool IsEmpty(struct Stack *s){
return !s->top;
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
struct Stack *stack = CreateStack(101);
int *ret = malloc(sizeof(int)*101);
*returnSize = 0;
struct TreeNode *p = root;
while(1){
while(p!=NULL){
ret[(*returnSize)++] = p->val;
Push(stack, p);
p = p->left;
}
if(IsEmpty(stack))return ret;
p = Pop(stack);
p = p->right;
}
}
这种更方便,直接利用栈的思想
int* preorderTraversal(struct TreeNode* root, int* returnSize){
struct TreeNode *stack[101];
int top = 0;
int *ret = malloc(sizeof(int)*101);
*returnSize = 0;
struct TreeNode *p = root;
while(1){
while(p!=NULL){
ret[(*returnSize)++] = p->val;
stack[top++] = p;
p = p->left;
}
if(top == 0)return ret;
p = stack[top-1];
top--;
p = p->right;
}
}
void postorder(struct TreeNode* root, int *ret, int *size){
if(root == 0)return;
postorder(root->left, ret, size);
postorder(root->right, ret, size);
ret[(*size)++] = root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize = 0;
int *ret = malloc(sizeof(int)*101);
postorder(root, ret, returnSize);
return ret;
}
struct TreeNode;
struct Stack{
struct TreeNode **stk;
int top;
};
struct Stack *CreateStack(int x){
struct Stack *create = malloc(sizeof(struct Stack));
create->stk = malloc(sizeof(struct TreeNode*)*x);
create->top = 0;
return create;
}
void Push(struct Stack *s, struct TreeNode *x){
s->stk[s->top++] = x;
}
struct TreeNode *Pop(struct Stack *s){
int t = s->top-1;
s->top--;
return s->stk[t];
}
bool IsEmpty(struct Stack *s){
return !s->top;
}
struct TreeNode *Peek(struct Stack *s){
return s->stk[s->top-1];
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize = 0;
int *ret = malloc(sizeof(int)*101);
struct Stack *stack = CreateStack(101);
struct TreeNode *p = root, *pre = NULL;
while(1){
while(p!=NULL){
Push(stack, p);
p = p->left;
}
if(IsEmpty(stack))return ret;
p = Peek(stack);
if(p->right == NULL || p->right == pre){
p = Pop(stack);
ret[(*returnSize)++] = p->val;
pre = p;
p = NULL;
}
else
{
p = p->right;
}
}
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
struct TreeNode *p = root;
struct TreeNode *stack[101];
int top = 0;
int *ret = malloc(sizeof(int)*101);
struct TreeNode *pre = NULL;
*returnSize = 0;
while(1){
while(p != NULL){
stack[top++] = p;
p = p->left;
}
if(top == 0)return ret;
p = stack[top-1];
if(p->right == NULL || p->right == pre){
ret[(*returnSize)++] = p->val;
top--;
pre = p;
p = NULL;
}
else
{
p=p->right;
}
}
}
void inorder(struct TreeNode *root, int *ret, int *size){
if(root ==0)
return;
inorder(root->left, ret, size);
ret[(*size)++] = root->val;
inorder(root->right, ret, size);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize = 0;
int *ret = malloc(sizeof(int)*101);
inorder(root, ret, returnSize);
return ret;
}
struct TreeNode;
struct Stack{
struct TreeNode **stk;
int top;
};
struct Stack *CreateStack(int x){
struct Stack *create = malloc(sizeof(struct Stack));
create->stk = malloc(sizeof(struct TreeNode*)*x);
create->top = 0;
return create;
}
void Push(struct Stack *s, struct TreeNode *x){
s->stk[s->top++] = x;
}
struct TreeNode *Pop(struct Stack *s){
int t = s->top-1;
s->top--;
return s->stk[t];
}
bool IsEmpty(struct Stack *s){
return !s->top;
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
struct Stack *stack = CreateStack(101);
struct TreeNode *p = root;
int *ret = malloc(sizeof(int)*101);
*returnSize = 0;
while(1){
while(p!=NULL){
Push(stack, p);
p = p->left;
}
if(IsEmpty(stack))return ret;
p = Pop(stack);
ret[(*returnSize)++] = p->val;
p = p->right;
}
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int *ret = malloc(sizeof(int)*101);
*returnSize = 0;
int top = 0;
struct TreeNode *p = root;
struct TreeNode *stack[101];
while(1){
while(p != NULL){
stack[top++] = p;
p = p->left;
}
if(top == 0)return ret;
p = stack[top-1];
ret[(*returnSize)++] = p->val;
top--;
p = p->right;
}
}