• 头歌实践平台-数据结构-二叉树及其应用


    第1关:实现二叉树的创建

    1. #include "binary_tree.h"
    2. BiTreeNode* CreatBiTree(char* s, int &i, int len)
    3. // 利用先序遍历创建二叉树
    4. // 参数:先序遍历字符串s,字符串初始下标i=0,字符串长度len。
    5. // 返回:二叉树
    6. {
    7. // 请在这里补充代码,完成本关任务
    8. /********** Begin *********/
    9. if(i>=len||s[i]=='#')return NULL;
    10. BiTreeNode*root=new BiTreeNode(s[i]);
    11. i++;
    12. root->left=CreatBiTree(s,i,len);
    13. i++;
    14. root->right=CreatBiTree(s,i,len);
    15. return root;
    16. /********** End **********/
    17. }
    18. void InOrder(BiTreeNode* root)
    19. // 二叉树的中序遍历
    20. // 参数:二叉树根节点root
    21. // 输出:中间没有空格,末尾不换行。
    22. {
    23. // 请在这里补充代码,完成本关任务
    24. /********** Begin *********/
    25. if(root==NULL)return;
    26. if(root->left!=NULL){
    27. InOrder(root->left);
    28. }
    29. printf("%c",root->data);
    30. if(root->right!=NULL){
    31. InOrder(root->right);
    32. }
    33. /********** End **********/
    34. }

    第2关:计算二叉树的深度和节点个数

    1. #include "binary_tree.h"
    2. int GetTreeDepth(BiTreeNode* root)
    3. // 计算该二叉树的深度
    4. // 参数:二叉树根节点root
    5. // 返回:二叉树的深度
    6. {
    7. // 请在这里补充代码,完成本关任务
    8. /********** Begin *********/
    9. int depthval,n,m;
    10. if (root==NULL) depthval=0;
    11. else{
    12. m=GetTreeDepth(root->left);
    13. n=GetTreeDepth(root->right);
    14. depthval=1+(m>n?m:n);
    15. }
    16. return depthval;
    17. /********** End **********/
    18. }
    19. int GetNodeNumber(BiTreeNode* root)
    20. // 计算该二叉树的总节点个数
    21. // 参数:二叉树根节点root
    22. // 返回:二叉树的总节点个数
    23. {
    24. // 请在这里补充代码,完成本关任务
    25. /********** Begin *********/
    26. int count,n,m;
    27. if(root==NULL) count= 0;
    28. else{
    29. m=GetNodeNumber(root->left);
    30. n=GetNodeNumber(root->right);
    31. count=m+n+1;
    32. }
    33. return count;
    34. /********** End **********/
    35. }
    36. int GetLeafNodeNumber(BiTreeNode* root)
    37. // 计算该二叉树的叶子节点个数
    38. // 参数:二叉树根节点root
    39. // 返回:二叉树的叶子节点个数
    40. {
    41. // 请在这里补充代码,完成本关任务
    42. /********** Begin *********/
    43. if (root==NULL) return 0;
    44. else if(root->left==NULL&&root->right==NULL) return 1;
    45. else return GetLeafNodeNumber(root->left)+ GetLeafNodeNumber(root->right);
    46. /********** End **********/
    47. }

    第3关:递归实现二叉树左右子树交换

    1. #include "binary_tree.h"
    2. BiTreeNode* BiTreeChange(BiTreeNode* root)
    3. // 实现二叉树左右子树的交换(递归法)
    4. // 参数:二叉树根节点root
    5. // 返回:二叉树
    6. {
    7. // 请在这里补充代码,完成本关任务
    8. /********** Begin *********/
    9. if (!root) return NULL;
    10. else
    11. {
    12. BiTreeNode* p=new BiTreeNode;
    13. p=root->left;
    14. root->left=root->right;
    15. root->right=p;
    16. BiTreeChange(root->left);
    17. BiTreeChange(root->right);
    18. }
    19. return root;
    20. /********** End **********/
    21. }
    22. void PreOrder(BiTreeNode* root)
    23. // 二叉树的前序遍历
    24. // 参数:二叉树根节点root
    25. // 输出:二叉树的前序遍历,中间没有空格,末尾不换行。
    26. {
    27. // 请在这里补充代码,完成本关任务
    28. /********** Begin *********/
    29. if (!root)
    30. {
    31. return;
    32. }else
    33. {
    34. printf("%c",root->data);
    35. PreOrder(root->left);
    36. PreOrder(root->right);
    37. }
    38. /********** End **********/
    39. }

    第4关:非递归实现二叉树左右子树交换

    1. #include "binary_tree.h"
    2. BiTreeNode* BiTreeChangeStack(BiTreeNode* root)
    3. // 实现二叉树左右子树的交换(栈实现)
    4. // 参数:二叉树根节点root
    5. // 返回:二叉树
    6. {
    7. // 请在这里补充代码,完成本关任务
    8. /********** Begin *********/
    9. if(!root) return NULL;
    10. stack s;
    11. s.push(root); //最后弹出保证根不变
    12. while(root&&!s.empty()) {
    13. BiTreeNode*p =new BiTreeNode;
    14. p=root->right;
    15. root->right=root->left;
    16. root->left=p;
    17. if(root->right)
    18. s.push(root->right);
    19. if(root->left){
    20. root=root->left;
    21. }else{
    22. root=s.top();
    23. s.pop();
    24. }
    25. }
    26. return root;
    27. /********** End **********/
    28. }
    29. void PostOrder(BiTreeNode* root)
    30. // 二叉树的后序遍历
    31. // 参数:二叉树根节点root
    32. // 输出:二叉树的后序遍历,中间没有空格,末尾不换行。
    33. {
    34. // 请在这里补充代码,完成本关任务
    35. /********** Begin *********/
    36. if(!root) return;
    37. else{
    38. PostOrder(root->left);
    39. PostOrder(root->right);
    40. printf("%c",root->data);
    41. }
    42. /********** End **********/
    43. }

    第5关:层次遍历二叉树

    1. #include "binary_tree.h"
    2. void HierarchyOrder(BiTreeNode* root)
    3. // 二叉树的层次遍历(队列实现)
    4. // 参数:二叉树根节点root
    5. // 输出:二叉树的层次遍历,中间没有空格,末尾不换行。
    6. {
    7. // 请在这里补充代码,完成本关任务
    8. /********** Begin *********/
    9. queue q; // 创建队列对象
    10. if(root!=NULL)
    11. q.push(root);
    12. while(!q.empty()) {
    13. printf("%c",q.front()->data);
    14. if(q.front()->left) q.push(q.front()->left);
    15. if (q.front()->right) q.push(q.front()->right);
    16. q.pop();
    17. }
    18. /********** End **********/
    19. }

  • 相关阅读:
    Chapter 03 你真的理解字典、集合吗?
    【无标题】
    计算机毕业设计Java房屋租赁平台(源码+系统+mysql数据库+lw文档)
    质量问题不是不爆,时候未到
    【DR_CAN-MPC学习笔记】1.最优化控制和MPC基本概念
    基于大数据的智能家居销量数据分析
    Vue脚手架
    AD9371 官方例程HDL详解(一)
    深度学习入门(四十七)计算机视觉——SSD和YOLO简介
    商务之理解项目目标
  • 原文地址:https://blog.csdn.net/toptopniba/article/details/134379470