二叉树的三种遍历

数据结构之二叉树——二叉树的遍历


一、二叉树的结构体表示

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
}TreeNode;

二、二叉树的前序遍历

1.递归表示

preorderTraversal函数

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int *list=malloc(sizeof(int)*501);
    *returnSize=0;
    preOrder(root,list,returnSize);
    return list;
}

preOrder函数

void preOrder(struct TreeNode* root,int* list,int* returnSize){
    if (root!=NULL)
    {
        list[(*returnSize)++]=root->val;
        preOrder(root->left,list,returnSize);
        preOrder(root->right,list,returnSize);
    } 
}

2.非递归表示

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    //定义TreeNode栈,并为该栈分配存储空间
    struct TreeNode **stack = malloc(sizeof(struct TreeNode*)*501);
    int top=0;
    //定义结果数组
    int* list = malloc(sizeof(int)*501);
    *returnSize=0;

    while (root!=NULL||top>0){
        if (root!=NULL){
            list[(*returnSize)++]=root->val;
            stack[top++]=root->right;
            root=root->left;
        }else{
            root=stack[--top];
        }
    }
    return list;
}

三、二叉树的中序遍历

1.递归表示

void InOrder(struct TreeNode* root,int* res,int* returnSize){
    if (root!=NULL){
        InOrder(root->left,res,returnSize);
        res[(*returnSize)++]=root->val;
        InOrder(root->right,res,returnSize);
    }
        
}

int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    int* res = malloc(sizeof(int)*501);
    *returnSize=0;
    InOrder(root,res,returnSize);
    return res;
}

2.非递归表示

int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    //为遍历返回的数组分配存储空间
    int* res = malloc(sizeof(int)*501);
    *returnSize=0;
    //初始化存储二叉树结点的栈,并为该栈分配存储空间
    struct TreeNode **stack = malloc(sizeof(struct TreeNode*)*501);
    int top=0;
    while (root!=NULL||top>0)
    {
        if(root!=NULL){
            stack[top++]=root;
            root=root->left;
        }else{
            root=stack[--top];
            res[(*returnSize)++]=root->val;
            root=root->right;
        }
    } 
    return res;
}

四、二叉树的后序遍历

1.递归表示

void posOrder(struct TreeNode* root,int* list,int* returnSize){
    if (root!=NULL)
    {
        posOrder(root->left,list,returnSize);
        posOrder(root->right,list,returnSize);
        list[(*returnSize)++]=root->val;
    }
    
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
    int* list=malloc(sizeof(int)*501);
    *returnSize=0;
    posOrder(root,list,returnSize);
    return list;
}

2.非递归表示

int* postorderTraversal(struct TreeNode* root, int* returnSize){
    //迭代遍历需要借助栈来辅助存储二叉树的结点
    //初始化TreeNode栈,并进行分配存储空间
    struct TreeNode **stack=malloc(sizeof(struct TreeNode*)*501);
    int top=0;
    int *list=malloc(sizeof(int)*501);
    *returnSize=0;

    struct TreeNode *pre=NULL;
    while (root!=NULL||top>0){
        while (root!=NULL){
            stack[top++]=root;
            root=root->left;
        }
        root=stack[--top];
        if (root->right==NULL||root->right==pre){
            list[(*returnSize)++]=root->val;
            pre=root;
            root=NULL;
        }else{
            stack[top++];
            root=root->right;
        }
    }
    return list;
}
```## 标题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值