二叉树的遍历(深度优先,广度优先)

二叉树的遍历:

 

深度优先搜索:

前序遍历,中序遍历,后序遍历

(前中后都是相对根而言的)

前:根左右

中:左根右

后:左右根

递归实现:

前序遍历:

typedef struct TreeNode{
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;
void inorderTree(TreeNode* root, int* nums, int* returnSize) {
    if(root == NULL)
        return ;
    res[(*returnSize)++] = root->data;
    inorderTree(root->left, nums, returnSize);
    inorderTree(root->right, nums, returnSize);
}
int* inorder(TreeNode* root, int* returnSize) {
    int* res = (int*)malloc(sizeof(int)*5001);
    *returnSize = 0;
    inorderTree(root, res, returnSize);
    return res;
}

中序遍历:

typedef struct TreeNode{
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;
void inorderTree(TreeNode* root, int* nums, int* returnSize) {
    if(root == NULL)
        return ;
    inorderTree(root->left, nums, returnSize);
    res[(*returnSize)++] = root->data;
    inorderTree(root->right, nums, returnSize);
}
int* inorder(TreeNode* root, int* returnSize) {
    int* res = (int*)malloc(sizeof(int)*5001);
    *returnSize = 0;
    inorderTree(root, res, returnSize);
    return res;
}

后序遍历:

typedef struct TreeNode{
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;
void inorderTree(TreeNode* root, int* nums, int* returnSize) {
    if(root == NULL)
        return ;
    inorderTree(root->left, nums, returnSize);
    inorderTree(root->right, nums, returnSize);
    res[(*returnSize)++] = root->data;
}
int* inorder(TreeNode* root, int* returnSize) {
    int* res = (int*)malloc(sizeof(int)*5001);
    *returnSize = 0;
    inorderTree(root, res, returnSize);
    return res;
}

迭代实现:

利用数据结构栈来实现,因为递归和栈原理是相同的,迭代实现只是将栈显式展现出来,递归是隐式实现

前序遍历的迭代实现:

迭代实现过程:

先将根节点读了

再读根节点的左子节点

如果没有左子节点就读根结点的右子节点

如下图的前序遍历为:1    2    4    5    3    6

 

进出栈的过程:

先将根节点读了再进栈再遍历左子节点

重复以上操作直到栈顶元素没有左子节点

检查栈顶元素是否有右子节点

如果有右子节点就读入并入栈

如果没有右子节点就将该结点出栈

重复这些操作这些操作直到栈空

void inorder(TreeNode* root) {
    TreeNode* stack = (TreeNode*)malloc(sizeof(TreeNode)*501);
    int top = 0;
    while(root != NULL || top>0) {
        while(root!=NULL) {
            printf("%d ", root->data);
            stack[top++] = root;
            root = root->left;
        }
        if(top>0) {
            root = stack[--top];
            root = root->right;
        }
    }
}

中序遍历的迭代实现:

迭代实现过程:

先读根结点的左子节点

再读根节点

最后读右子节点

如下图中序遍历为:4    2    5    1    3    6

 

如下图中序遍历为:4    8    7    9    2    5    1    3    6

 

进出栈过程:

先将根节点入栈再看左子节点

重复以上操作直到没有左子节点

将该节点读入并检查该节点是否有右子节点

  • 如果有右子结点 则重头开始

  • 如果没有则将该节点出栈

重复以上操作

void inorder(TreeNode* root) {
    TreeNode* stack = (TreeNode*)malloc(sizeof(TreeNode)*501);
    int top = 0;
    while(root != NULL || top>0) {
        while(root!=NULL) {
            stack[top++] = root;    // 进栈
            root = root->left;
        }
        if(top>0) {
            root = stack[--top];    // 出栈
            printf("%d ", root->data);
            root = root->right;
        }
    }
}

后序遍历的迭代实现:

迭代实现过程:

先将根节点的左子节点读入

再读入根节点的右子节点

最后读入根节点

如下图所示后序遍历是:8    9    7    4    5    2    6    3    1

 

进出栈过程:

先将根节点入栈再看左子节点

重复以上操作直到没有左子节点

检查是否有有右子节点

  • 如果有右子节点将其入栈

  • 如果没有右子节点就将该节点读入并将其出栈

重复以上操作直到栈空

void inorder(TreeNode* root) {
    TreeNode** stack = (TreeNode*)malloc(sizeof(TreeNode*)*501);
    int top = 0;
    while(root != NULL || top>0) {
        while(root!=NULL) {
            stack[top++] = root;    // 进栈
            root = root->left;
        }
        root = stack[--top];
        if(root->right != NULL) {
            stack[top++] = root->right;
            root = root->right;
        }else{
            printf("%d ", root->data);
            root = stack[--top];
        }
    }
}

广度优先搜索:

前,中,后序遍历

使用数据结构队列来实现

队列实现过程(模仿了横切)

 

使用队列就是一层一层的读入,1,2,3,4,5的实现过程

void sequenceOrder(TreeNode* root) {
    TreeNode** queue = (TreeNode*)malloc(sizeof(TreeNode*)*501);
    int front = 0, rear = 0;
    while(root != NULL || rear != front) {
        if(root != NULL)
            printf("%d ", root->data);
        queue[rear++] = root;
        queue[rear++] = root->left;
        queue[rear++] = root->right;
        root = queue[++front];
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值