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

给出一颗二叉树,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 }

 

posted @ 2022-02-18 19:53  std&ice  阅读(86)  评论(0编辑  收藏  举报