ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

二叉树模板合集(前中后层序遍历 中后序->前序 前中序->后序)

2022-02-18 20:00:30  阅读:238  来源: 互联网

标签:string 后序 int 前序 dfs substr 二叉树 first


给出一颗二叉树,n个节点,然后n行,每行3个数字,表示父节点,左儿子,右儿子

 

 遍历模板:

传送门

  1 /**\
  2 二叉树的前中后序遍历
  3 前序:根左右
  4 中序:左根右
  5 后序:左右根
  6 
  7 input:
  8 6
  9 1 2 3
 10 2 4 5
 11 3 6 -1
 12 4 -1 -1
 13 5 -1 -1
 14 6 -1 -1
 15 
 16 output:
 17 124536
 18 425163
 19 452631
 20 123456
 21 
 22 \**/
 23 #include <bits/stdc++.h>
 24 
 25 using namespace std;
 26 
 27 struct node
 28 {
 29     int l, r;
 30 } t[300];
 31 
 32 int n;
 33 int first;
 34 
 35 //前序遍历
 36 void dfs1(int x)
 37 {
 38     if(x == -1) return ;
 39     cout << x;     //根
 40     dfs1(t[x].l);  //左
 41     dfs1(t[x].r);  //右
 42 }
 43 
 44 //中序遍历
 45 void dfs2(int x)
 46 {
 47     if(x == -1) return ;
 48     dfs2(t[x].l); //左
 49     cout << x;    //中
 50     dfs2(t[x].r); //右
 51 }
 52 
 53 //后序遍历
 54 void dfs3(int x)
 55 {
 56     if(x == -1) return ;
 57 
 58     dfs3(t[x].l); //左
 59     dfs3(t[x].r); //右
 60     cout << x;    //后
 61 }
 62 
 63 //层次遍历
 64 void bfs()
 65 {
 66     queue<int> q;
 67     q.push(first);
 68     while(!q.empty())
 69     {
 70         int now = q.front();
 71         cout << now;
 72         q.pop();
 73         if(t[now].l != -1) q.push(t[now].l);
 74         if(t[now].r != -1) q.push(t[now].r);
 75     }
 76 }
 77 
 78 
 79 signed main()
 80 {
 81     cin >> n;
 82 
 83     cin >> first;
 84     cin >> t[first].l >> t[first].r;
 85 
 86     int a, b, c;
 87 
 88     for(int i = 2; i <= n; ++i)
 89     {
 90         cin >> a >> b >> c;
 91         t[a].l = b;
 92         t[a].r = c;
 93     }
 94 
 95     dfs1(first), putchar('\n');
 96     dfs2(first), putchar('\n');
 97     dfs3(first), putchar('\n');
 98     
 99     bfs();
100 
101     return 0;
102 }

 

中后序->前序

传送门

利用后序遍历的最后一个元素就是根节点,来对中序遍历进行切分。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 string pre, mid;
 6 
 7 void dfs(string p, string m)
 8 {
 9     if(p.empty()) return; 
10 
11     char root = p[p.size() - 1];
12     int k = m.find(root);
13 
14     p.erase(p.end() - 1);
15 
16     string pl = p.substr(0, k);
17     string pr = p.substr(k);
18     string ml = m.substr(0, k);
19     string mr = m.substr(k + 1);
20 
21     printf("%c", root);
22     dfs(pl, ml);
23     dfs(pr, mr);
24 }
25 
26 signed main()
27 {
28 
29 
30     cin >> mid >> pre;
31     dfs(pre, mid);
32     return 0;
33 }

 

前中序->后序

传送门

利用前序遍历的第一个元素就是根节点,来对中序遍历进行切分。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 string pre, mid;  //前序 中序
 6 
 7 void dfs(string p, string m)
 8 {
 9     if(p.empty()) return ; //空序列返回
10 
11     char root = p[0]; //p[0]前序首字符就是根节点
12 
13     int k = m.find(root); //找到中序中 root的位置
14 
15     p.erase(p.begin());
16 
17     // 分隔前序 中序 序列
18     string pl = p.substr(0, k);
19     string pr = p.substr(k);
20     string ml = m.substr(0, k);
21     string mr = m.substr(k + 1);
22 
23     //后序 左右根
24     dfs(pl, ml);
25     dfs(pr, mr);
26     printf("%c", root);
27 
28 
29 }
30 
31 signed main()
32 {
33     cin >> mid >> pre;
34     dfs(pre, mid);
35     return 0;
36 }

 

标签:string,后序,int,前序,dfs,substr,二叉树,first
来源: https://www.cnblogs.com/-ytz/p/15911115.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有