标签:
剑指offer第六题:根据给出的前序和中序遍历序列重构二叉树
1 //============================================================================ 2 // Name : JZ-C-06.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 #include <iomanip> 12 #include <exception> 13 using namespace std; 14 15 typedef struct BinaryTreeNode { //二叉树结点 16 int data; //数据域 17 struct BinaryTreeNode *left; //左孩子 18 struct BinaryTreeNode *right; //右孩子 19 } BTN; 20 BTN* ConstructBTCore(int *startPreOrder, int *endPreOrder, int *startInOrder, 21 int *endInOrder); //函数调用在前,定义在后,需要先进行函数原型说明 22 23 BTN* ConstructBT(int *preOrder, int *inOrder, int length) { 24 if (preOrder == NULL || inOrder == NULL || length <= 0) { 25 cout << "序列为空?请检查两个序列" << endl; 26 return NULL; 27 } 28 BTN *root = ConstructBTCore(preOrder, preOrder + length - 1, inOrder, 29 inOrder + length - 1); 30 cout << root->data << endl;//返回根 31 return root; 32 } 33 34 BTN* ConstructBTCore(int *startPreOrder, int *endPreOrder, int *startInOrder, 35 int *endInOrder) { 36 BTN *root = new BTN(); //生成结点 37 root->data = startPreOrder[0]; 38 root->left = root->right = NULL; 39 if (endPreOrder == startPreOrder) { 40 if (startInOrder == endInOrder && *startPreOrder == *startInOrder) { 41 return root; 42 } else { 43 cout << "前序序列和中序序列不匹配,错误!" << endl; 44 throw std::exception(); 45 } 46 } 47 int inOrderLength = endInOrder - startInOrder; //中序遍历序列长度 48 int i = 0; 49 for (; i < inOrderLength + 1; i++) { //在中序遍历序列里查找头结点的值 50 // int rootInOrder =*startInOrder; 51 // while(*startInOrder+rootInOrder){} 52 if (*startPreOrder == *(startInOrder + i)) { //和前序遍历序列的头结点比较 53 root->data = *startPreOrder; //头结点 54 if (i > 0) { //如果有左子树 55 root->left = ConstructBTCore(startPreOrder + 1, 56 startPreOrder + i, startInOrder, startInOrder + i - 1); //递归遍历左子树序列 57 } 58 if (i < inOrderLength) { //如果有右子树 59 root->right = ConstructBTCore(startPreOrder + i + 1, 60 endPreOrder, startInOrder + i + 1, endInOrder); //递归遍历右子树序列 61 } 62 break; //退出循环 63 } 64 } 65 if (i == inOrderLength + 1) { //说明中序遍历序列中没有和前序遍历序列头结点值相同的结点,错误 66 cout << "中序序列中没有前序序列的头结点,错误!" << endl; 67 throw std::exception(); 68 } 69 return root; 70 } 71 int main() { 72 const int length = 7; //完全二叉树 73 int preorder[length] = { 1, 2, 4, 5, 3, 6, 7 }; 74 int inorder[length] = { 4, 2, 5, 1, 6, 3, 7 }; 75 // const int length = 8;//普通二叉树 76 // int preorder[length] = { 1, 2, 4, 7, 3, 5, 6, 8 }; 77 // int inorder[length] = { 4, 7, 2, 1, 5, 3, 8, 6 }; 78 // const int length = 7;//不匹配的序列 79 // int preorder[length] = {1, 2, 4, 5, 3, 6, 7}; 80 // int inorder[length] = {4, 2, 8, 1, 6, 3, 7}; 81 // const int length = 5;//所有结点都没有右子结点 82 // int preorder[length] = { 1, 2, 3, 4, 5 }; 83 // int inorder[length] = { 5, 4, 3, 2, 1 }; 84 // const int length = 5;// 所有结点都没有左子结点 85 // int preorder[length] = {1, 2, 3, 4, 5}; 86 // int inorder[length] = {1, 2, 3, 4, 5}; 87 // const int length = 1;//树中只有一个结点 88 // int preorder[length] = {1}; 89 // int inorder[length] = {1}; 90 ConstructBT(preorder, inorder, length); 91 // ConstructBT(NULL, NULL, 0);//输入空指针 92 93 return 0; 94 }
标签:
原文地址:http://www.cnblogs.com/Laughing-Lz/p/5505475.html