重建二叉树
// 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。 #include <iostream> #include <exception> //二叉树结构体 struct BinaryTreeNode{ int value;//根 BinaryTreeNode *pLeft;//左 BinaryTreeNode *pRight;//右 }; //打印二叉树 void PrintTreeNode(const BinaryTreeNode*pNode) { if (pNode != nullptr) { printf("value of this node is : %d\n", pNode->value); if (pNode->pLeft != nullptr) { printf("value of its left child is : %d\n", pNode->pLeft->value); } else{ printf("left child is nullptr...\n"); } if (pNode->pRight != nullptr) { printf("value of its right child is : %d\n", pNode->pRight->value); } else{ printf("right child is nullptr...\n"); } } else{ printf("this node is nullptr...\n"); } printf("\n"); } void PrintTree(const BinaryTreeNode* pRoot) { PrintTreeNode(pRoot); if (pRoot != nullptr) { if (pRoot->pLeft != nullptr) PrintTree(pRoot->pLeft); if (pRoot->pRight != nullptr) PrintTree(pRoot->pRight); } } //销毁二叉树 void DestroyTree(BinaryTreeNode* pRoot) { if (pRoot != nullptr) { BinaryTreeNode* pLeft = pRoot->pLeft; BinaryTreeNode* pRight = pRoot->pRight; delete pRoot; pRoot = nullptr; DestroyTree(pLeft); DestroyTree(pRight); } } BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder) { int rootValue = startPreorder[0];//前序遍历第一个节点是根节点 BinaryTreeNode* root = new BinaryTreeNode(); root->value = rootValue; root->pLeft = nullptr; root->pRight = nullptr; if (startPreorder == endPreorder) //只有一个根节点 { if ((startInorder == endInorder) && (startPreorder[0] == startInorder[0])) { return root; } else{ throw std::exception("Invalid input...111111"); } } //从中序遍历中寻找根节点,左边为根节点的左子树,右边为根节点的右子树 int* rootInorde = startInorder; while (rootInorde <= endInorder && (*rootInorde != rootValue)) { rootInorde++; } if (rootInorde == endInorder && (*rootInorde != rootValue))//中序遍历完,没有找到根节点的值 { throw std::exception("Invalid input...222222"); } int leftLength = rootInorde - startInorder;//中序遍历中,根节点左边有多少个元素(左子树) int* leftPreorderEnd = startPreorder + leftLength; if (leftLength > 0) { root->pLeft = ConstructCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorde - 1); } if (leftLength < endPreorder - startPreorder) { root->pRight = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorde + 1, endInorder); } return root; } // ============================测试代码========================== int Test(char* testName, int*preorder, int*inorder, int length) { if (preorder == nullptr || inorder == nullptr || length <= 0) { std::cout << "input error..." << std::endl; return 1; } if (testName != nullptr) { std::cout << testName << " begins:\n"; } std::cout << "The preorder sequence is:" << std::endl; for (int i = 0; i < length; i++) { std::cout << preorder[i] << " "; } std::cout << std::endl; std::cout << "The inorder sequence is:" << std::endl; for (int i = 0; i < length; i++) { std::cout << inorder[i] << " "; } std::cout << std::endl; //调用重建函数 BinaryTreeNode* root = ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1); PrintTree(root); DestroyTree(root); return 0; } void Test1() { const int length = 8; int preorder[length] = { 1, 2, 4, 7, 3, 5, 6, 8 }; int inorder[length] = { 4, 7, 2, 1, 5, 3, 8, 6 }; Test("Test1", preorder, inorder, length); } void Test2() { const int length = 5; int preorder[length] = { 1, 2, 3, 4, 5 }; int inorder[length] = { 5, 4, 3, 2, 1 }; Test("Test2", preorder, inorder, length); } int main() { Test1(); }
从尾到头打印链表
#include <iostream> #include <stack> using namespace std; struct ListNode{ int value; struct ListNode* pNext; }; void PrintListReversinglyIteratively(ListNode* pHead) { stack<ListNode*> nodes; ListNode* pNode = pHead; while (pNode != nullptr) { nodes.push(pNode); pNode = pNode->pNext; } //输出栈 while (!nodes.empty()) { pNode = nodes.top();//取出栈顶元素 cout << "value=" << pNode->value << ", "; nodes.pop();//弹出栈顶元素 } } //方法二:递归法 void PrintListReversinglyRecursively(ListNode* pHead) { if (pHead != nullptr) { if (pHead->pNext != nullptr) { PrintListReversinglyRecursively(pHead->pNext); } cout << "value=" << pHead->value << ","; } } //打印链表 void PrintList(ListNode* pHead) { cout << "print list starts...\n" << endl; ListNode* pNode = pHead; while (pNode != nullptr) { cout << "print list value=" << pNode->value << endl; pNode = pNode->pNext; } cout << "print list ends...\n" << endl; } ListNode* CreateListNode(int value) { ListNode* pNode = new ListNode(); pNode->value = value; pNode->pNext = nullptr; return pNode; } void ConnectListNodes(ListNode* pCurrent, ListNode* pNext) { if (pCurrent == nullptr) { cout << "error to connect two nodes" << endl; exit(-1); } pCurrent->pNext = pNext; } void DestroyList(ListNode* pHead) { ListNode* pNode = pHead; while (pNode != nullptr) { pHead = pHead->pNext; delete pNode; pNode = pHead; } } // ====================测试代码==================== void Test(ListNode* pHead) { PrintList(pHead); PrintListReversinglyIteratively(pHead); printf("\n\n"); PrintListReversinglyRecursively(pHead); } void Test1() { cout << endl << "Test1 begins..." << endl; ListNode* pNode1 = CreateListNode(1); ListNode* pNode2 = CreateListNode(2); ListNode* pNode3 = CreateListNode(3); ListNode* pNode4 = CreateListNode(4); ListNode* pNode5 = CreateListNode(5); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode4); ConnectListNodes(pNode4, pNode5); Test(pNode1); DestroyList(pNode1); } int main() { Test1(); return 0; }