标签:后序 ++ col bre tree post struct return private
1 class tree{ 2 private: 3 struct TNode{ 4 int date; 5 TNode *lchild, *rchild; 6 }; 7 8 9 /*vector<int> post; 10 vector<int> in;*/ 11 12 int post[50]; 13 int in[50]; 14 15 public: 16 17 18 tree(){ 19 20 21 int N; 22 cin >> N; 23 for(int i = 0; i < N; i++){ 24 cin >> in[i]; 25 } 26 for(int i = 0; i < N; i++){ 27 cin >> post[i]; 28 } 29 30 TNode *T = createTree(0, N - 1, 0, N - 1); 31 32 Print(T); 33 34 } 35 36 TNode * createTree(int postL, int postR, int inL, int inR){ 37 38 if(postL > postR){ 39 return NULL; 40 } 41 TNode *root = new TNode; 42 root->date = post[postR]; 43 int k; 44 for(k = inL; k <= inR; k++){ 45 if(in[k] == post[postR]) 46 break; 47 } 48 int numLeft = k - inL; 49 root->lchild = createTree(postL, postL + numLeft - 1, inL, k - 1); 50 root->rchild = createTree(postL + numLeft, postR - 1, k + 1, inR); 51 return root; 52 } 53 54 55 56 57 ~tree(){ } 58 };
标签:后序 ++ col bre tree post struct return private
原文地址:https://www.cnblogs.com/MasterYan576356467/p/11223383.html