鱼C论坛

 找回密码
 立即注册
查看: 1390|回复: 0

[技术交流] PTA A_1020 Tree Traversals

[复制链接]
发表于 2020-2-9 16:44:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 798236606 于 2020-2-9 16:43 编辑

传送门:https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072

解:
1.二叉查找树递归插入+广度优先遍历
  1. #include<cstdio>
  2. #include<queue>

  3. using namespace std;

  4. typedef struct node{
  5.     int data;
  6.     struct node *lchild, *rchild;
  7.    
  8.     node()
  9.     {
  10.         lchild = rchild = NULL;
  11.     }
  12. }BTNode, *BTREE;

  13. int n, postorder[35], inorder[35];

  14. int find(int target)
  15. {
  16.     for (int i = 0; i < n; i++)
  17.         if (inorder[i] == target)
  18.             return i;
  19.             
  20.     return -1;
  21. }

  22. void insert(BTREE root, int pos)
  23. {
  24.     if (!root) return;
  25.    
  26.     int parents = find(root->data);
  27.    
  28.     if (pos > parents)
  29.     {
  30.         if (root->rchild)
  31.             insert(root->rchild, pos);
  32.         else
  33.         {
  34.             root->rchild = new BTNode;
  35.             root->rchild->data = inorder[pos];
  36.         }
  37.     }
  38.     else
  39.     {
  40.         if (root->lchild)
  41.             insert(root->lchild, pos);
  42.         else
  43.         {
  44.             root->lchild = new BTNode;
  45.             root->lchild->data = inorder[pos];
  46.         }           
  47.     }
  48. }

  49. void BFS(BTREE root)
  50. {
  51.     queue<BTREE> q;
  52.    
  53.     q.push(root);
  54.    
  55.     while (1)
  56.     {
  57.         printf("%d", q.front()->data);
  58.         
  59.         if (q.front()->lchild) q.push(q.front()->lchild);
  60.         if (q.front()->rchild) q.push(q.front()->rchild);
  61.         q.pop();
  62.         
  63.         if (q.empty()) break;

  64.         putchar(' ');
  65.     }
  66. }

  67. int main(void)
  68. {
  69.     scanf("%d", &n);
  70.    
  71.     if (!n) return 0;
  72.    
  73.     for (int i = 0; i < n; i++)
  74.         scanf("%d", postorder + i);
  75.         
  76.     for (int i = 0; i < n; i++)
  77.         scanf("%d", inorder + i);
  78.    
  79.     BTREE root = new BTNode;   
  80.     root->data = postorder[n - 1];
  81.    
  82.     for (int i = n - 2; i >= 0; i--)
  83.         insert(root, find(postorder[i]));
  84.         
  85.     BFS(root);

  86.     return 0;   
  87. }
复制代码

2.二叉查找树迭代插入+广度优先遍历
  1. #include<cstdio>
  2. #include<queue>

  3. using namespace std;

  4. typedef struct node{
  5.     int data;
  6.     struct node *lchild, *rchild;

  7.     node()
  8.     {
  9.         lchild = rchild = NULL;
  10.     }
  11. } BTNode, *BTREE;

  12. int n, postorder[35], inorder[35];

  13. int find(int target)
  14. {
  15.     for (int i = 0; i < n; i++)
  16.         if (inorder[i] == target)
  17.             return i;

  18.     return -1;
  19. }

  20. void insert(BTREE root, int pos)
  21. {
  22.     if (!root) return;

  23.     BTREE child = new BTNode;
  24.     child->data = inorder[pos];

  25.     while (1)
  26.     {
  27.         if (pos > find(root->data))
  28.         {
  29.             if (root->rchild)
  30.                 root = root->rchild;
  31.             else
  32.             {
  33.                 root->rchild = child;
  34.                 break;
  35.             }
  36.         }
  37.         else
  38.         {
  39.             if (root->lchild)
  40.                 root = root->lchild;
  41.             else
  42.             {
  43.                 root->lchild = child;
  44.                 break;
  45.             }
  46.         }
  47.     }
  48. }

  49. void BFS(BTREE root)
  50. {
  51.     queue<BTREE> q;

  52.     q.push(root);

  53.     while (1)
  54.     {
  55.         printf("%d", q.front()->data);

  56.         if (q.front()->lchild) q.push(q.front()->lchild);
  57.         if (q.front()->rchild) q.push(q.front()->rchild);
  58.         q.pop();

  59.         if (q.empty()) break;

  60.         putchar(' ');
  61.     }
  62. }

  63. int main(void)
  64. {
  65.     scanf("%d", &n);

  66.     if (!n) return 0;

  67.     for (int i = 0; i < n; i++)
  68.         scanf("%d", postorder + i);

  69.     for (int i = 0; i < n; i++)
  70.         scanf("%d", inorder + i);

  71.     BTREE root = new BTNode;
  72.     root->data = postorder[n - 1];

  73.     for (int i = n - 2; i >= 0; i--)
  74.         insert(root, find(postorder[i]));

  75.     BFS(root);

  76.     return 0;
  77. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-27 13:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表