前序遍历:先访问当前节点,再访问当前节点的左子树,最后访问当前节点的右子树。对于二叉树,深度遍历与此同。规律:根在前;子树在根后且左子树比右子树靠前,且第一个就是根节点;
中序遍历:先访问当前节点的左子树,然后访问当前节点,最后是当前节点的右子树,二叉树,中序遍历会得到数据升序效果。规律:根在中;左子树在跟左边,右子树在根右边,左边部分是根结点的左子树的中序遍历序列,右边部分是根结点的右子树的中序遍历序列 ;
后序遍历:先访问当前节点的左子树,然后是当前节点的又子树,最后是当前节点。规律:根在后;子树在根前且左子树比右子树靠前,且最后一个节点是根节点。
前序+中序还原二叉树
- 根据前序序列的第一个元素建立根结点;
- 在中序序列中找到该元素,确定根结点的左右子树的中序序列;
- 在前序序列中确定左右子树的前序序列;
- 由左子树的前序序列和中序序列建立左子树;
- 由右子树的前序序列和中序序列建立右子树。
如:已知一棵二叉树的先序遍历序列和中序遍历序列分别是abdgcefh、dgbaechf,求二叉树及后序遍历序列。
先序:abdgcefh—>a bdg cefh
中序:dgbaechf—->dgb a echf
得出结论:a是树根,a有左子树和右子树,左子树有bdg结点,右子树有cefh结点。
先序:bdg—>b dg
中序:dgb —>dg b
得出结论:b是左子树的根结点,b无右子树,有左子树。
先序:dg—->d g
中序:dg—–>dg
得出结论:d是b左子树的根节点,d无左子树,g是d的右子树
然后对于a 的右子树类似可以推出
最后还原+后序遍历+高度代码实现
#include <iostream>
#include <string>
using namespace std;
typedef struct bintree{
struct bintree* lchild;
struct bintree*rchild;
char val;
}Bintree;
Bintree *settree(string arr, string arr2,int len){
if (len == NULL) return NULL;
Bintree* root = new Bintree;
int i;
root->val = arr[0]; //前序遍历第一个为根节点
for (i = 0; i < len; ++i){ //中序遍历中根节点左边为左子树,右边为有子树
if (arr2[i] == arr[0])
break;
}
root->lchild=settree(arr.substr(1,i), arr2, i);//左子树
//右子树
root->rchild = settree(arr.substr(i + 1, len - i - 1),
arr2.substr(i + 1, len - i - 1), len - i - 1);
return root;
}
int Gettree(Bintree* root){ //得到树的高度
int H, L, R;
if (root == NULL) return 0;
else{
L = Gettree(root->lchild);
R = Gettree(root->rchild);
H = L > R ? L + 1 : R + 1;
}
return H;
}
void aftertree(Bintree* root){ //后续遍历
if (root == NULL) return;
else{
aftertree(root->lchild);
aftertree(root->rchild);
cout << root->val;
}
}
int main(){
string arr;
string arr2;
int len;
while (cin>>len){
cin >> arr; //输入前序遍历
cin >> arr2; //输入中序遍历
Bintree *root = settree(arr, arr2, len);
aftertree(root);
cout << endl;
int hight = Gettree(root);
cout << hight << endl;
}
}