1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| #include <cstdio> #include <queue> #include <vector> using namespace std;
struct node{ node* lchild; node* rchild; int data; };
int n; vector<int> pre; vector<int> in; vector<int> post; vector<int> layer;
node* createByPostAndIn(int postL, int postR, int inL, int inR){ if(postL > postR) return NULL; node* root = new node; root->data = post[postR]; int k; for(k = inL; k <= inR; k++){ if(in[k] == post[postR]){ break; } } int numLeft = k - inL; root->lchild = createByPostAndIn(postL, postL + numLeft - 1, inL, k - 1); root->rchild = createByPostAndIn(postL + numLeft, postR - 1, k + 1, inR); return root; }
node* createByPreAndIn(int preL, int preR, int inL, int inR){ if(preL > preR) return NULL; node* root = new node; root->data = pre[preL]; int k; for(k = inL; k <= inR; k++){ if(in[k] == pre[preL]){ break; } } int numLeft = k - inL; root->lchild = createByPreAndIn(preL + 1, preL + numLeft, inL, k - 1); root->rchild = createByPreAndIn(preL + numLeft + 1, preR, k + 1, inR); return root; }
node* createByLayerAndIn(vector<int> layer, vector<int> in, int inL, int inR){ if(inL > inR || layer.size() == 0) return NULL; node* root = new node; root->data = layer[0]; int pos; for(pos = inL; pos <= inR; pos++){ if(in[pos] == layer[0]){ break; } } vector<int> layerLeft, layerRight; for(int i = 1; i < layer.size(); i++){ int j; for(j = inL; j < pos; j++){ if(in[j] == layer[i]){ layerLeft.push_back(layer[i]); break; } } if(j == pos) layerRight.push_back(layer[i]); } root->lchild = createByLayerAndIn(layerLeft, in, inL, pos - 1); root->rchild = createByLayerAndIn(layerRight, in, pos + 1, inR); return root; }
int num = 0; void BFS(node* root){ queue<node*> q; q.push(root); while(!q.empty()){ node* now = q.front(); q.pop(); printf("%d",now->data); num++; if(num < n){ printf(" "); } if(now->lchild != NULL) q.push(now->lchild); if(now->rchild != NULL) q.push(now->rchild); } }
void preOrder(node* root){ if(root == NULL){ return; } printf("%d",root->data); num++; if(num < n){ printf(" "); } preOrder(root->lchild); preOrder(root->rchild); }
void inOrder(node* root){ if(root == NULL){ return; } inOrder(root->lchild); printf("%d",root->data); num++; if(num < n){ printf(" "); } inOrder(root->rchild); }
void postOrder(node* root){ if(root == NULL){ return; } postOrder(root->lchild); postOrder(root->rchild); printf("%d",root->data); num++; if(num < n){ printf(" "); } }
int main(){ scanf("%d",&n); int temp; for(int i = 0; i < n; i++){ scanf("%d",&temp); in.push_back(temp); }
for(int i = 0; i < n; i++){ scanf("%d",&temp); post.push_back(temp); } node* root = createByPostAndIn(0, n -1, 0, n -1);
BFS(root); return 0; }
|
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !