Data Structure--二叉树例题解析(2)--二叉树前序遍历--二叉树中序遍历--二叉树后序遍历

谁借莪1个温暖的怀抱¢ 2022-10-31 13:50 311阅读 0赞

二叉树例题解析2

    • 二叉树前序遍历 递归
    • 二叉树前序遍历 非递归
    • 二叉树中序遍历 非递归
    • 二叉树后序遍历 非递归

这里的三种遍历方法和之前讲的 遍历方法也有关,有兴趣可以回顾一下.

二叉树前序遍历 递归

我只写了前序遍历的递归方式,但是只要你将前序的掌握了,后面两种的递归方式也是大同小异,大家自己写哦.

  1. int getSize(struct TreeNode* root){ //求大小的函数
  2. if (root){
  3. return getSize(root->left) + getSize(root->right) + 1;
  4. }
  5. return 0;
  6. }
  7. void preorder(struct TreeNode* root, int* arr, int* idx){
  8. if (root){
  9. arr[*idx] = root->val; //核心 将值进行赋予,并进行递归的操作
  10. (*idx)++;
  11. preorder(root->left, arr, idx); //左子树
  12. preorder(root->right, arr, idx); //右子树
  13. }
  14. }
  15. int* preorderTraversal(struct TreeNode* root, int* returnSize){
  16. int sz = getSize(root); //先求长
  17. int* arr = (int*)malloc(sizeof(int)*sz); //动态开辟
  18. int idx = 0;
  19. preorder(root, arr, &idx); //函数调用
  20. *returnSize = idx; //更新
  21. return arr; //输出
  22. }

二叉树前序遍历 非递归

调用了栈的操作,接口我就不多描述了, 具体点击

  1. int* preorderTraversal(struct TreeNode* root, int* returnSize){
  2. int sz = getSize(root); //先求长
  3. int* arr = malloc(sizeof(int)*sz); //动态开辟
  4. stack st; //栈开辟
  5. stackInit(&st); //初始化
  6. int idx; //定义整型变量
  7. //当前遍历的节点不为空,或栈不为空
  8. while (root || !stackEmpty(&st)){
  9. //1.访问左边路径
  10. while (root){ //1.====前序
  11. arr[idx++] = root->val;
  12. //当前节点入栈
  13. stackPush(&st, root); //2.====先访问左子树
  14. root = root->left;
  15. }
  16. //2.获取栈顶元素,访问右子树
  17. root = stackTop(&st); //3.====再头结点,并出栈
  18. stackPop(&st);
  19. root = root->right; //4.====再访问右子树
  20. }
  21. *returnSize = idx;
  22. return arr; //返回值
  23. }

二叉树中序遍历 非递归

大题思路一致,只是访问的先后次序发生了变化

  1. int* inorderTraversal(struct TreeNode* root, int* returnSize){
  2. int sz = getSize(root); //得到大小
  3. int* arr = malloc(sizeof(int)*sz); //动态开辟
  4. stack st; //创建栈
  5. stackInit(&st); //初始化
  6. int idx = 0;
  7. while (root || !stackEmpty(&st)){ //存在并不为空的条件下开始循环
  8. //1.====中序遍历
  9. //1.先遍历左边
  10. while (root){
  11. stackPush(&st, root); //2.====左子树
  12. root = root->left;
  13. }
  14. //2.获取栈顶元素
  15. root = stackTop(&st); //3.====栈顶
  16. stackPop(&st);
  17. arr[idx++] = root->val;
  18. //3.访问右子树
  19. root = root->right; //4.====右子树
  20. }
  21. *returnSize = idx;
  22. return arr;
  23. }

二叉树后序遍历 非递归

在这里插入图片描述

  1. int* postorderTraversal(struct TreeNode* root, int* returnSize){
  2. int sz = getSize(root);
  3. int* arr = (int*)malloc(sizeof(int)*sz);
  4. stack st;
  5. stackInit(&st);
  6. int idx = 0;
  7. //prev:上次访问记得节点位置
  8. struct TreeNode* prev = NULL;
  9. while (root || !stackEmpty(&st)){
  10. //1.遍历左路径
  11. while (root){
  12. stackPush(&st, root); //1.====左子树
  13. root = root->left;
  14. }
  15. //top:栈顶的节点
  16. struct TreeNode* top = stackTop(&st);
  17. //这里要进行判断是否能访问栈顶
  18. //条件:没有右子树或者右子树访问完毕
  19. if (top->right == NULL || top->right == prev){ //2.====判断是否为头结点
  20. //访问栈顶元素
  21. arr[idx++] = top->val;
  22. stackPop(&st);
  23. prev = top;
  24. }
  25. else
  26. //访问右子树
  27. root = top->right; //3.====右子树
  28. }
  29. *returnSize = idx;
  30. return arr;
  31. }

主要还是这个后序遍历比较难一点,前面的都好理解,大家注意理解后序遍历访问了头结点但是没有进行出栈,而是先考虑右子树,再反回去对头结点进行出栈操作,注意理解思路.
做敲代码,大家一起加油!!!马上开学了,我们也要努力每天敲代码哦!!!

发表评论

表情:
评论列表 (有 0 条评论,311人围观)

还没有评论,来说两句吧...

相关阅读