ICode9

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

已知二叉树的前序(或后序)和中序遍历求这颗二叉树

2022-04-18 19:02:00  阅读:161  来源: 互联网

标签:pre 遍历 int 中序 二叉树 post include BinTree 前序


具体实现请看代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<vector>
  5 #include<map>
  6 #include<queue>
  7 #include<set>
  8 #include<cmath>
  9 #include<list>
 10 #include<cstring>
 11 #include<string>
 12 #define ll long long
 13 #define ull unsigned long long
 14 #define inf 0x3f3f3f3f
 15 #define inff 0x7fffffff
 16 using namespace std;
 17 const int N = 30 + 10;
 18 const int M = 200000 + 10;
 19 const ll mod = 1e9 + 7;
 20 
 21 //pre数组存放前序遍历序列,in数组存放中序遍历序列,post数组存放后序遍历序列
 22 int pre[N], in[N], post[N];
 23 
 24 typedef struct node* BinTree;
 25 
 26 struct node {
 27     int date;
 28     BinTree left;
 29     BinTree right;
 30 };
 31 
 32 //已知二叉树的先序遍历和中序遍历求这颗二叉树
 33 BinTree Build_pre(int pre[], int in[], int size) {
 34 
 35     if (size <= 0) return NULL;
 36     int rem = 0;
 37     for (int i = 0; i < size; i++) {
 38         if (in[i] == pre[0]) {
 39             rem = i;
 40             break;
 41         }
 42     }
 43     BinTree tree = (BinTree)malloc(sizeof(struct node));
 44     tree->date = pre[0];
 45     tree->left = Build_pre(pre + 1, in, rem);
 46     tree->right = Build_pre(pre + rem + 1, in + rem + 1, size - rem - 1);
 47 
 48     return tree;
 49 }
 50 
 51 //已知二叉树的后续遍历和中序遍历求这颗二叉树
 52 BinTree Build_post(int post[], int in[], int size) {
 53 
 54     if (size <= 0) return NULL;
 55     int rem = 0;
 56     for (int i = 0; i < size; i++) {
 57         if (in[i] == post[size - 1]) {
 58             rem = i;
 59             break;
 60         }
 61     }
 62     BinTree tree = (BinTree)malloc(sizeof(struct node));
 63     tree->date = post[size - 1];
 64     tree->left = Build_post(post, in, rem);
 65     tree->right = Build_post(post + rem, in + rem + 1, size - rem - 1);
 66 
 67     return tree;
 68 }
 69 
 70 //以后序遍历这颗二叉树
 71 void postorder(BinTree T) {
 72 
 73     if (T == NULL) return;
 74     else {
 75         postorder(T->left);
 76         postorder(T->right);
 77         cout << T->date << " ";
 78     }
 79 
 80     return;
 81 }
 82 
 83 //以前序遍历这颗二叉树
 84 void preorder(BinTree T) {
 85 
 86     if (T == NULL) return;
 87     else {
 88         cout << T->date << " ";
 89         preorder(T->left);
 90         preorder(T->right);
 91     }
 92 
 93     return;
 94 }
 95 
 96 //以层序遍历这颗二叉树
 97 void sequence(BinTree T) {
 98 
 99     if (T == NULL) return;
100     queue<BinTree>q;
101     q.push(T);
102     while (!q.empty()) {
103         BinTree tmp = q.front();
104         q.pop();
105         if (q.size() == 0 && tmp->left == NULL && tmp->right == NULL) cout << tmp->date;
106         else cout << tmp->date << " ";
107         if (tmp->left != NULL) q.push(tmp->left);
108         if (tmp->right != NULL) q.push(tmp->right);
109     }
110 
111     return;
112 }
113 
114 int main() {
115 
116     int n;
117     cin >> n;
118     for (int i = 0; i < n; i++) {
119         cin >> post[i];
120     }
121     for (int i = 0; i < n; i++) {
122         cin >> in[i];
123     }
124     BinTree T;
125     T = Build_post(post, in, n);
126     //sequence(T);
127     preorder(T);
128     //postorder(T);
129 
130     return 0;
131 }

 

 

标签:pre,遍历,int,中序,二叉树,post,include,BinTree,前序
来源: https://www.cnblogs.com/wabi/p/16161106.html

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

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

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

ICode9版权所有