题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
来源:互联网 发布:德州扑克入门知乎 编辑:程序博客网 时间:2024/06/12 01:24
1》 stdafx.h
<span style="font-family: Arial, Helvetica, sans-serif;">#pragma once</span>
#include "targetver.h"#include <stdio.h>#include <tchar.h>
2>targetver.h
#pragma once// The following macros define the minimum required platform. The minimum required platform// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run // your application. The macros work by enabling all features available on platform versions up to and // including the version specified.// Modify the following defines if you have to target a platform prior to the ones specified below.// Refer to MSDN for the latest info on corresponding values for different platforms.#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.#endif
3>BinaryTree.h
#pragma oncestruct BinaryTreeNode{int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; };_declspec(dllexport)void PrintTree(BinaryTreeNode *pRoot);_declspec(dllexport)void DestroyTree(BinaryTreeNode *pRoot);
4>BinaryTree.cpp
#include "stdafx.h"#include "BinaryTree.h"void PrintTreeNode(BinaryTreeNode *pNode);void PrintTree(BinaryTreeNode *pRoot){PrintTreeNode(pRoot);// if(pRoot != NULL){if(pRoot->m_pLeft != NULL)PrintTree(pRoot->m_pLeft);if(pRoot->m_pRight != NULL)PrintTree(pRoot->m_pRight);}}void PrintTreeNode(BinaryTreeNode *pNode){if(pNode != NULL){printf("value of this node is : %d\n", pNode->m_nValue);if(pNode->m_pLeft != NULL)printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);elseprintf("left child is null.\n");if(pNode->m_pRight != NULL)printf("value of its right childe is : %d.\n", pNode->m_pRight->m_nValue);elseprintf("right child is null.\n");}else{printf("this node is null.\n");}printf("\n");}void DestroyTree(BinaryTreeNode *pRoot){if(pRoot != NULL){BinaryTreeNode *pLeft = pRoot->m_pLeft;BinaryTreeNode *pRight = pRoot->m_pRight;delete pRoot; pRoot = NULL;DestroyTree(pLeft);DestroyTree(pRight);}}
5>ConstructBinaryTree.cpp
/*题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。*/#include "stdafx.h"#include "BinaryTree.h"//#include "iostream"// 不能是 iostream.husing namespace std;/*preorder 前序遍历inorder中序遍历*/BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder);BinaryTreeNode *Construct(int *preorder, int *inorder, int length){if(preorder == NULL || inorder == NULL || length <= 0)return NULL;return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length -1);}// startPreorder 前序遍历的第一个节点// endPreorder 前序遍历的左后一个节点// startInorder 中序遍历的第一个节点// startInorder 中序遍历的最后一个节点BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder){ // 前序遍历序列的第一个数字是根结点的值int rootValue = startPreorder[0];BinaryTreeNode *root = new BinaryTreeNode();root->m_nValue = rootValue;root->m_pLeft = root->m_pRight = NULL;if(startPreorder == endPreorder){if(startInorder == endInorder && *startPreorder == *startInorder)return root;elsethrow std::exception("Invalid input.");}//// 在中序遍历中找到根结点的值int *rootInorder = startInorder;while(rootInorder <= endInorder && *rootInorder != rootValue)++rootInorder;if(rootInorder == endInorder && *rootInorder != rootValue)throw std::exception("Invalid input");//int leftLength = rootInorder - startInorder;//int rightLength = endInorder - rootInorder;int *leftPreorderEnd = startPreorder + leftLength;if(leftLength > 0){// 构建左子树root->m_pLeft = ConstructCore(startPreorder +1, leftPreorderEnd, startInorder, rootInorder -1);}if(leftLength < endPreorder -startPreorder){// 构建右子树root->m_pRight = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);}//return root;}// 测试代码void Test(char *testName, int *preorder, int *inorder, int length){if(testName != NULL)printf("%s Begins:\n", testName);printf("The preorder sequence is: ");for(int i = 0; i < length; ++i)printf("%d ", preorder[i]);printf("\n");printf("The inorder sequence is:");for(int i = 0; i < length; ++i)printf("%d ", inorder[i]);printf("\n");try{BinaryTreeNode *root = Construct(preorder, inorder,length);PrintTree(root);DestroyTree(root);}catch(std::exception &expection){printf("Invalid Input.\n");}}// 普通二叉树// 1// / \// 2 3 // / / \// 4 5 6// \ /// 7 8void 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);}// 所有结点都没有右子结点// 1// / // 2 // / // 3 // /// 4// /// 5void 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);}// 所有结点都没有左子结点// 1// \ // 2 // \ // 3 // \// 4// \// 5void Test3(){const int length = 5;int preorder[length] = {1, 2, 3, 4, 5,};int inorder[length] = {1, 2, 3, 4, 5};Test("Test3", preorder ,inorder, length);}// 树中只有一个结点void Test4(){const int length = 1;int preorder[length] = {1};int inorder[length] = {1};Test("Test4", preorder, inorder, length);}// 完全二叉树// 1// / \// 2 3 // / \ / \// 4 5 6 7/*判断完全二叉树:完全二叉树:除最后一层外,每一层上的节点数均达到最大值;在最后一层上只缺少右边的若干结点。完全二叉树(Complete Binary Tree)定义: 若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。*/void Test5(){const int length = 7;int preorder[length] = {1, 2, 4, 5, 3, 6, 7};int inorder[length] = {4, 2, 5, 1, 6, 3, 7};Test("Test5", preorder, inorder, length);}// 输入空指针void Test6(){Test("Test6", NULL, NULL, 0);}// 输入的两个序列不匹配void Test7(){const int length = 7; int preorder[length] = {1, 2, 4, 5, 3, 6, 7}; int inorder[length] = {4, 2, 8, 1, 6, 3, 7};Test("Test7: for unmatched input", preorder, inorder, length);}int _tmain(int argc, _TCHAR *argv[]){//Test1();// Test2();//Test3();//Test4();//Test6();Test7();//Test5();system("pause");return 0;}
0 0
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 输入某二叉树的前序遍历和中序遍历的结果请重建出该二叉树。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。
- java 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 《剑指Offer》 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
- 根据二叉树的前序遍历和中序遍历的结果,请重建出该二叉树
- 输入二叉树的前序遍历和中序遍历,重建出该二叉树
- 输入某二叉树的前序和中序遍历结果,重建该二叉树
- 输入某二叉树的前序遍历和中序遍历的结果,重建该二叉树
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5
- 函数指针问题,求解答
- PS讲6-----选区的布尔运算
- 开启红米2手机AGPS辅助,让红米2定位更快
- Java泛型编程使用总结
- C语言中的字符串
- 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 简单的对话聊天窗体组件
- A Simple Problem with Integers
- “秋老虎”肆虐 要做到四个“当心”
- apache24设置虚拟目录 (windows)
- 列表,添加, 删除简单处理
- [Leetcode 6] ZigZag问题的一种新思路
- 【Leetcode】Remove Element #27
- Volley详解(五)——网络(Network)