1. 程式人生 > >OpenJudge 由中根順序和後根序列重建二叉樹

OpenJudge 由中根順序和後根序列重建二叉樹

cout reorder cin fetch http new build ostream out

技術分享圖片

技術分享圖片

【題解】

後根序列的最後一個元素即為二叉樹的樹根root。root將中根序列分為兩部分,左半邊是左子樹的中根序列,而右半邊則是右部分的中根序列。同時後根序列依照左子樹和右子樹節點數也可以被分為左子樹的後根序列和右子樹的後根序列。於是便可依此遞歸地按左右子樹的後根、中根序列重建子樹,最終重建二叉樹。

【代碼】

 1 #include <iostream>
 2 using namespace std;
 3 /*build BT from its inorder and postorder*/
 4 
 5 
 6 constexpr int MAXN = 65540
; 7 int postorder[MAXN], inorder[MAXN]; 8 9 struct btn 10 { 11 int data; 12 btn *left; 13 btn *right; 14 }; 15 16 btn *build_tree(int io1, int io2, int po1, int po2) 17 { 18 /*io1, io2 are the begining and ending points of inorder sequence respectively*/ 19 /*po1, po2 are the begining and ending points of postorder sequence respectively
*/ 20 int i = 0, ion = io2 - io1 + 1; 21 btn* root = new btn; 22 root->data = postorder[po2]; 23 root->left = NULL; 24 root->right = NULL; 25 for (i = 0; i < ion; i++) { 26 if (root->data == inorder[io1 + i])break; 27 } 28 if (i > 0) root->left = build_tree(io1, io1 + i - 1
, po1, po1 + i - 1); 29 if (io1 + i < io2) root->right = build_tree(io1 + i + 1, io2, po1 + i, po2 - 1); 30 return root; 31 } 32 33 void preorder(btn *root, int vnum, int &count) 34 { 35 if (root != NULL) { 36 cout << root->data; 37 if (count < vnum - 1) { 38 cout << " "; 39 count++; 40 preorder(root->left, vnum, count); 41 preorder(root->right, vnum, count); 42 } 43 else cout << endl; 44 } 45 } 46 47 void deletetree(btn *root) 48 { 49 if (root != NULL) { 50 delete(root->left); 51 delete(root->right); 52 delete root; 53 root = NULL; 54 } 55 } 56 57 int main() 58 { 59 int c = 0, i = 0, vnum = 0; 60 while (cin >> inorder[i++]) { 61 if (cin.get() != )break; 62 /*Alththough cin will skip the blank space, the cursor stays at the blank after cin reads a number,thus cin.get() can fetch the 63 blank space.*/ 64 } 65 66 vnum = i; 67 i = 0; 68 while (cin >> postorder[i++]) { 69 if (cin.get() != )break; 70 } 71 btn *root = build_tree(0, i - 1, 0, i - 1); 72 preorder(root, vnum, c); 73 deletetree(root); 74 return 0; 75 }

OpenJudge 由中根順序和後根序列重建二叉樹