海涛老师的面试题-作业6-重建二叉树
View Code
1 // 重建二叉树.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 using namespace std; 7 8 9 struct BinaryTreeNode 10 { 11 int m_nValue; 12 BinaryTreeNode* m_pLeft; 13 BinaryTreeNode* m_pRight; 14 }; 15 16 17 18 BinaryTreeNode* ConstructCore(int* StartPreorder,int* EndPreorder,int* StartInorder,int* EndInorder) 19 { 20 int RootValue=StartPreorder[0]; 21 BinaryTreeNode* Root=new BinaryTreeNode(); 22 Root->m_nValue=RootValue; 23 Root->m_pLeft=Root->m_pRight=NULL; 24 if(StartPreorder==EndPreorder) 25 { 26 if(StartInorder==EndInorder&&*StartPreorder==*StartInorder) 27 { 28 return Root; 29 } 30 else 31 { 32 throw exception("Invalid Input!"); 33 } 34 } 35 int* RootInorder=StartInorder; 36 while(RootInorder<=EndInorder&&*RootInorder!=RootValue) 37 ++RootInorder; 38 if(RootInorder==EndInorder&&*RootInorder!=RootValue) 39 throw exception("Invalid Input!"); 40 int LeftLength=RootInorder-StartInorder; 41 int* LeftPreorderEnd=StartPreorder+LeftLength; 42 if(LeftLength>0) 43 { 44 Root->m_pLeft=ConstructCore(StartPreorder+1,LeftPreorderEnd,StartInorder,RootInorder-1); 45 } 46 if(LeftLength<EndPreorder-StartPreorder) 47 { 48 Root->m_pRight=ConstructCore(LeftPreorderEnd+1,EndPreorder,RootInorder+1,EndInorder); 49 } 50 return Root; 51 } 52 53 54 BinaryTreeNode* Construct(int* preorder,int*inorder,int length) 55 { 56 if(preorder==NULL||inorder==NULL||length<=0) 57 return NULL; 58 return ConstructCore(preorder,preorder+length-1,inorder,inorder+length-1); 59 } 60 61 62 void PrintTree(BinaryTreeNode* Head) 63 { 64 if(Head) 65 { 66 if(Head->m_pLeft) 67 PrintTree(Head->m_pLeft); 68 if(Head->m_pRight) 69 PrintTree(Head->m_pRight); 70 cout<<Head->m_nValue<<" "; 71 } 72 else 73 return; 74 } 75 76 void DestroyTree(BinaryTreeNode* Head) 77 { 78 if(Head) 79 { 80 if(Head->m_pLeft) 81 DestroyTree(Head->m_pLeft); 82 else if(Head->m_pRight) 83 DestroyTree(Head->m_pRight); 84 delete Head; 85 } 86 else 87 return; 88 } 89 90 91 void Test(char* testName, int* preorder, int* inorder, int length) 92 { 93 if(testName != NULL) 94 printf("%s begins:\n", testName); 95 96 printf("The preorder sequence is: "); 97 for(int i = 0; i < length; ++ i) 98 printf("%d ", preorder[i]); 99 printf("\n"); 100 101 printf("The inorder sequence is: "); 102 for(int i = 0; i < length; ++ i) 103 printf("%d ", inorder[i]); 104 printf("\n"); 105 106 try 107 { 108 BinaryTreeNode* root = Construct(preorder, inorder, length); 109 PrintTree(root); 110 cout<<endl; 111 DestroyTree(root); 112 } 113 catch(exception& exception) 114 { 115 printf("Invalid Input.\n"); 116 } 117 } 118 119 120 // 普通二叉树 121 // 1 122 // / \ 123 // 2 3 124 // / / \ 125 // 4 5 6 126 // \ / 127 // 7 8 128 129 void Test1() 130 { 131 const int length = 8; 132 int preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8}; 133 int inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6}; 134 135 Test("Test1", preorder, inorder, length); 136 } 137 138 139 // 所有结点都没有右子结点 140 // 1 141 // / 142 // 2 143 // / 144 // 3 145 // / 146 // 4 147 // / 148 // 5 149 void Test2() 150 { 151 const int length = 5; 152 int preorder[length] = {1, 2, 3, 4, 5}; 153 int inorder[length] = {5, 4, 3, 2, 1}; 154 155 Test("Test2", preorder, inorder, length); 156 } 157 158 // 所有结点都没有左子结点 159 // 1 160 // \ 161 // 2 162 // \ 163 // 3 164 // \ 165 // 4 166 // \ 167 // 5 168 void Test3() 169 { 170 const int length = 5; 171 int preorder[length] = {1, 2, 3, 4, 5}; 172 int inorder[length] = {1, 2, 3, 4, 5}; 173 174 Test("Test3", preorder, inorder, length); 175 } 176 177 // 树中只有一个结点 178 void Test4() 179 { 180 const int length = 1; 181 int preorder[length] = {1}; 182 int inorder[length] = {1}; 183 184 Test("Test4", preorder, inorder, length); 185 } 186 187 // 完全二叉树 188 // 1 189 // / \ 190 // 2 3 191 // / \ / \ 192 // 4 5 6 7 193 void Test5() 194 { 195 const int length = 7; 196 int preorder[length] = {1, 2, 4, 5, 3, 6, 7}; 197 int inorder[length] = {4, 2, 5, 1, 6, 3, 7}; 198 199 Test("Test5", preorder, inorder, length); 200 } 201 202 // 输入空指针 203 void Test6() 204 { 205 Test("Test6", NULL, NULL, 0); 206 } 207 208 // 输入的两个序列不匹配 209 void Test7() 210 { 211 const int length = 7; 212 int preorder[length] = {1, 2, 4, 5, 3, 6, 7}; 213 int inorder[length] = {4, 2, 8, 1, 6, 3, 7}; 214 215 Test("Test7: for unmatched input", preorder, inorder, length); 216 } 217 218 219 220 221 222 223 int _tmain(int argc, _TCHAR* argv[]) 224 { 225 Test1(); 226 Test2(); 227 Test3(); 228 Test4(); 229 Test5(); 230 Test6(); 231 Test7(); 232 return 0; 233 }