已知二叉树的先序(后序)遍历和中序遍历建立二叉树

已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?

对一棵二叉树进行遍历,我们可以采取3中顺序进行遍历,分别是前序遍历、中序遍历和后序遍历。这三种方式是以访问父节点的顺序来进行命名的。假设父节点是N,左节点是L,右节点是R,那么对应的访问遍历顺序如下:

 

  • 前序遍历    N->L->R
  • 中序遍历    L->N->R
  • 后序遍历    L->R->N

/*****************************************************************************************************/
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢!
/*****************************************************************************************************/

所以,对于以下这棵树,三种遍历方式的结果是:
 
  • 前序遍历    ABCDEF
  • 中序遍历    CBDAEF
  • 后序遍历    CDBFEA

已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历


其实,只要知道其中任意两种遍历的顺序,我们就可以推断出剩下的一种遍历方式的顺序,这里我们只是以:知道前序遍历和中序遍历,推断后序遍历作为例子,其他组合方式原理是一样的。要完成这个任务,我们首先要利用以下几个特性:
  • 特性A,对于前序遍历,第一个肯定是根节点;
  • 特性B,对于后序遍历,最后一个肯定是根节点;
  • 特性C,利用前序或后序遍历,确定根节点,在中序遍历中,根节点的两边就可以分出左子树和右子树;
  • 特性D,对左子树和右子树分别做前面3点的分析和拆分,相当于做递归,我们就可以重建出完整的二叉树;
我们以一个例子做一下这个过程,假设:
  • 前序遍历的顺序是: CABGHEDF
  • 中序遍历的顺序是: GHBACDEF
第一步,我们根据特性A,可以得知根节点是C,然后,根据特性C,我们知道左子树是:GHBA,右子树是:DEF。
                        C
                      /     \
               GHBA   DEF
第二步,取出左子树,左子树的前序遍历是:ABGH,中序遍历是:GHBA,根据特性A和C,得出左子树的父节点是A,并且A没有右子树。
                        C
                      /     \
                   A    DEF
                 /
            GBH
第三步,使用同样的方法,前序是BGH,中序是GBH,得出父节点是B,G和H分别是左右节点。
                       C
                      /     \
                   A    DEF
                 /
            B
          /    \
       G      H
第四步,回到右子树,它的前序是EDF,中序是DEF,依然根据特性A和C,得出父节点是E,左右节点是D和F。
                      C
                      /     \
                   A        E
                 /          /    \
            B           D      F
          /    \
       G      H
到此,我们得到了这棵完整的二叉树,因此,它的后序遍历就是:GHBADFEC。

因此我们按照上面的思路,采用递归的方法建树即可完成,下面给出代码,并有例题,有兴趣者可以去试试!


先序+中序建树过程:

//如果不知道substr函数作用的同学自行百度吧

void build1(node* &t, string pre, string mid)
{
    if(pre.length() == 0) {
        t = NULL;
        return;
    }
    char f = pre[0]; //根节点
    int idx = mid.find(f);
    string ml = mid.substr(0, idx); //左子树的中序
    string mr = mid.substr(idx+1);  //右子树的中序
    int L = ml.length();
    int R = mr.length();
    string pl = pre.substr(1, L); //左子树的前序
    string pr = pre.substr(L+1);  //右子树的前序
    t = new node;
    if(t) {
        t->data = f;
        build1(t->l, pl, ml); //递归建造左子树
        build1(t->r, pr, mr); //递归建造右子树
    }
}


例题:POJ 2255-Tree Recovery

AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

string mid, pre;

struct node
{
    int data;
    node *l;
    node *r;
};

void build(node* &t, string pre, string mid)
{
    if(pre.length() == 0) {
        t = NULL;
        return;
    }
    char f = pre[0];
    int idx = mid.find(f);
    string ml = mid.substr(0, idx);
    string mr = mid.substr(idx+1);
    int L = ml.length();
    int R = mr.length();
    string pl = pre.substr(1, L);
    string pr = pre.substr(L+1);
    t = new node;
    if(t != NULL) {
        t->data = f;
        build(t->l, pl, ml);
        build(t->r, pr, mr);
    }
}

void post_vis(node *t)
{
    if(t != NULL) {
        post_vis(t->l);
        post_vis(t->r);
        printf("%c", t->data);
    }
}

int main()
{
    while(cin>>pre>>mid) {
        node *tree;
        build(tree, pre, mid);
        post_vis(tree);
        printf("\n");
    }
    return 0;
}

另外可能我们有时候还会碰到已知后序+中序让你建树的题目,这个前面的差不多,只是在求左右子树的时候稍有不同,下面也给出代码:

void build2(node* &t, string post, string mid)
{
    if(post.length() == 0) {
        t = NULL;
        return;
    }
    int len = post.length();
    char f = post[len-1];
    int idx = mid.find(f);
    string ml = mid.substr(0, idx);
    string mr = mid.substr(idx+1);
    int L = ml.length();
    int R = mr.length();
    string pl = post.substr(0, L);
    string pr = post.substr(L, R);
    t = new node;
    if(t) {
        t->data = f;
        build2(t->l, pl, ml);
        build2(t->r, pr, mr);
    }
}
最后给出两种方式建树和遍历的测试代码,并附有测试数据:DBACEGF ABCDEFG ACBFGED
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

string mid, pre, post;

struct node
{
    int data;
    node *l;
    node *r;
};

//如果不知道substr函数作用的同学自行百度吧

void build1(node* &t, string pre, string mid)
{
    if(pre.length() == 0) {
        t = NULL;
        return;
    }
    char f = pre[0]; //根节点
    int idx = mid.find(f);
    string ml = mid.substr(0, idx); //左子树的中序
    string mr = mid.substr(idx+1);  //右子树的中序
    int L = ml.length();
    int R = mr.length();
    string pl = pre.substr(1, L); //左子树的前序
    string pr = pre.substr(L+1);  //右子树的前序
    t = new node;
    if(t) {
        t->data = f;
        build1(t->l, pl, ml); //递归建造左子树
        build1(t->r, pr, mr); //递归建造右子树
    }
}

void build2(node* &t, string post, string mid)
{
    if(post.length() == 0) {
        t = NULL;
        return;
    }
    int len = post.length();
    char f = post[len-1];
    int idx = mid.find(f);
    string ml = mid.substr(0, idx);
    string mr = mid.substr(idx+1);
    int L = ml.length();
    int R = mr.length();
    string pl = post.substr(0, L);
    string pr = post.substr(L, R);
    t = new node;
    if(t) {
        t->data = f;
        build2(t->l, pl, ml);
        build2(t->r, pr, mr);
    }
}

void pre_vis(node *t)
{
    if(t) {
        printf("%c", t->data);
        pre_vis(t->l);
        pre_vis(t->r);
    }
}

void mid_vis(node *t)
{
    if(t) {
        mid_vis(t->l);
        printf("%c", t->data);
        mid_vis(t->r);
    }
}

void post_vis(node *t)
{
    if(t) {
        post_vis(t->l);
        post_vis(t->r);
        printf("%c", t->data);
    }
}

void cen_vis(node *t)
{
    queue<node*> q;
    q.push(t);
    while(!q.empty()) {
        node *cur;
        cur = q.front();
        q.pop();
        if(cur) {
            printf("%c", cur->data);
            q.push(cur->l);
            q.push(cur->r);
        }
    }
}

int main()
{
    while(cin>>pre>>mid>>post) {
        node *tree1, *tree2;
        build1(tree1, pre, mid);
        printf("先序+中序建树完成!\n");
        printf("先序遍历:");
        pre_vis(tree1);
        printf("\n");
        printf("中序遍历:");
        mid_vis(tree1);
        printf("\n");
        printf("后序遍历:");
        post_vis(tree1);
        printf("\n");
        printf("层序遍历:");
        cen_vis(tree1);
        printf("\n");

        build2(tree2, post, mid);
        printf("后序+中序建树完成!\n");
        printf("先序遍历:");
        pre_vis(tree2);
        printf("\n");
        printf("中序遍历:");
        mid_vis(tree2);
        printf("\n");
        printf("后序遍历:");
        post_vis(tree2);
        printf("\n");
        printf("层序遍历:");
        cen_vis(tree2);
        printf("\n");
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/i_believe_cwj/article/details/80408905