代码随想录训练营DAY14 || 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历

144. 二叉树的前序遍历

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;
    }
}

 

 145. 二叉树的后序遍历

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;
        }
    }
}

 

 94. 二叉树的中序遍历

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值