# 二叉查找树的前序遍历，后序遍历和中序遍历互求算法模板

```#include <iostream>
#include <string>

using std::string;

template<typename _Val>
struct TreeNodeBase
{
TreeNodeBase *left;
TreeNodeBase *right;
_Val val;
};

//已知前序遍历和中序遍历，找后序遍历
template<typename _Iter, typename _sizeType, typename _TreeNodeType = TreeNodeBase<char>>
_TreeNodeType *preOrderInfixOrderToSufixOrder(_Iter &pre, _Iter &in, _sizeType length)
{
if (length == 0)
return nullptr;

auto node = new _TreeNodeType;

_sizeType index = 0;

for (; index != length; index++)//找到对应的前序遍历的点
if (*(in + index) == *(pre))
break;

node->left = preOrderInfixOrderToSufixOrder(pre + 1, in, index);
node->right = preOrderInfixOrderToSufixOrder(pre + index + 1, in + index + 1, length - (index + 1));
node->val = *pre;
std::cout << node->val << " ";
return node;
}

//已知后序遍历和中序遍历，找前序遍历
template<typename _Iter, typename _sizeType, typename _TreeNodeType = TreeNodeBase<char>>
_TreeNodeType *sufixOrderInfixOrderToPreOrder(_Iter &sufix, _Iter &in, _sizeType length)
{
if (length == 0)
return nullptr;
auto node = new _TreeNodeType;
node->val = *(sufix + length - 1);
std::cout << node->val << " ";

_sizeType index = 0;
for (;index != length ;index++)
if (*(in + index) == *(sufix + length - 1))
break;
node->left = sufixOrderInfixOrderToPreOrder(sufix, in, index);
node->right = sufixOrderInfixOrderToPreOrder(sufix + index, in + index + 1, length - (index + 1));

return node;
}

/*
//已知前序遍历/后序遍历 + 中序遍历求后序遍历/前序遍历
int main()
{
string preOrder("GDAFEMHZ");
string sufixOrder("AEFDHZMG");

auto root1 = preOrderInfixOrderToSufixOrder(preOrder.begin(), infixOrder.begin(), preOrder.size());
std::cout << std::endl;
auto root2 = sufixOrderInfixOrderToPreOrder(sufixOrder.begin(), infixOrder.begin(), preOrder.size());

system("pause");
return 0;
}*/```

## 根据前序遍历和中序遍历构建二叉树以及根据中序遍历后序遍历构建二叉树

<pre name="code" class="cpp">/************************************************************************/ /* 算法说明: 由中序遍历序列可知,第一个节点是根节点, 由前序遍历序列可知,第一个节点是根节点的左子树节点,而且前序遍历中,根节点左边是左子树,右边是右子树,因此通过中序遍历的根节点可以确定的是: 根节点在前序遍历中的位置(通过遍历前序遍历序列,

## hdu1710-Binary Tree Traversals （由二叉树的先序序列和中序序列求后序序列）

http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4210    Accepted Submission(s): 1908 Problem Description A binary tree is a

## 根据二叉树的先序序列和中序序列还原二叉树并打印后序序列

#include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; struct Node { int value; Node *left; Node *right; Node(int value) { this->value = value; left = right = NULL; } }; bool bNotTree = false; Node* RebuildTree(i

## HDU 1710 二叉树遍历，输入前、中序求后序

1.HDU  1710  Binary Tree Traversals 2.链接:http://acm.hust.edu.cn/vjudge/problem/33792 3.总结:记录下根结点,再拆分左右子树,一直搜下去.感觉像dfs. 题意:二叉树,输入前.中序求后序. (1)建立出一颗二叉树,更直观.但那些指针用法并不是很懂. #include<iostream> #include<cstdio> #include<cstdlib> using namespace

## 图的建立及输出其先序序列，中序序列，后续序列

1 #include<stdio.h> 2 #include<malloc.h> 3 typedef struct node{ 4 char data; 5 struct node *left; 6 struct node *right; 7 }Node; 8 9 int i = 0; 10 char str[100]; 11 12 void create(Node** p){ 13 if(str[i] == '\0') 14 return ; 15 if(str[i++] !=

## 由先序序列和中序序列生成一棵二叉树

#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct BiTNode{ char e; struct BiTNode *lchild,*rchild; }BiTNode; void preOrderTravse(BiTNode *T1){ if(T1){ printf("%c",T1->e); preOrderTravse(T1->lchild); p