二叉树--由中序遍历和后续遍历重建二叉树

来源:互联网 发布:网络主播签约注意事项 编辑:程序博客网 时间:2024/05/16 01:13
  • 题目:传送门

    描述
    我们知道如何按照三种深度优先次序来周游一棵二叉树,来得到中根序> 列、前根序列和后根序列。反过来,如果给定二叉树的中根序列和后根> 序列,或者给定中根序列和前根序列,可以重建一二叉树。本题输入一> 棵二叉树的中根序列和后根序列,要求在内存中重建二叉树,最后输出> 这棵二叉树的前根序列。
    用不同的整数来唯一标识二叉树的每一个结点,下面的二叉树

    中根序列是9 5 32 67
    后根序列9 32 67 5
    前根序列5 9 67 32

    输入两行。第一行是二叉树的中根序列,第二行是后根序列。每个数字表示> 的结点之间用空格隔开。结点数字范围0~65535。暂不必考虑不合理> 的输入数据。

    输出二叉树前序遍历结果

    样例输入
    9 5 32 67
    9 32 67 5

    样例输出
    5 9 67 32

  • 分析:用后序找到根节点,然后用中序根据根节点找到左右子树,知道找到所有节点

  • 代码
/*@Filename:      code.cpp@Author:        wyl6 @Mail:          ustbcs_wyl@163.com思路:用后序找到根节点,然后用中序根据根节点找到左右子树,知道找到所有节点*/#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;const int MAXN = 1e5;int inOrder[MAXN],postOrder[MAXN];struct binaryNode{    int data;    binaryNode *left,*right;    binaryNode():data(0),left(NULL),right(NULL){}};binaryNode* BuildTree(int inB,int inE,int postB,int postE){    int i = 0;    binaryNode * root = new binaryNode();    root->data = postOrder[postE];  //后序遍历序列中获取根节点    while(inOrder[inB+i] != root->data) i++;//中序遍历序列中查找子树根节点位置    if(i>0)//左子树存在        root->left = BuildTree(inB,inB+i-1,postB,postB+i-1);    if(inB+i<inE)//右子树存在        root->right = BuildTree(inB+i+1,inE,postB+i,postE-1);    return root;}void PreTravel(binaryNode * root){    if(root == NULL) return;    printf("%d ",root->data);    PreTravel(root->left);    PreTravel(root->right);}void DeleteTree(binaryNode * root){    if(root == NULL) return;    DeleteTree(root->left);    DeleteTree(root->right);    delete root;}int main(){    int i = 0;    while(cin >> inOrder[i++]) //get到每次读入一行的新方法        if(cin.get() != ' ') break;    i = 0;    while(cin >> postOrder[i++])        if(cin.get() != ' ') break;    binaryNode * root = BuildTree(0,i-1,0,i-1);    PreTravel(root);    DeleteTree(root);    return 0;}


  • 参考

1.已知二叉树的中根和后根序列怎么确定一棵树(图解例题)

2.由中根序列和后根序列重建二叉树

3.刘大爷的紫书第6章

阅读全文
0 0