其他分享
首页 > 其他分享> > 已知二叉树的前序(或后序)和中序遍历求这颗二叉树

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

作者:互联网

具体实现请看代码:

  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