关于二叉树:树专题Binary-Tree-Traversals

30次阅读

共计 2578 个字符,预计需要花费 7 分钟才能阅读完成。

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
Output
For each test case print a single line specifying the corresponding postorder sequence.

Sample Input
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstdio>
#include<queue>
#include<stack> 
#include<set> 
#include<vector> 
using namespace std;
/*
先依照先序和中序创立树,而后对失去的树进行后序遍历 
*/ 
int pre[1010],in[1010],ans[1010];// 先序数组和中序数组,失去的后续数组 
int k;// 用来输入失去的树所用的下标 
struct node{
    int value;
    node *l,*r;
    node(int value = 0,node *l = NULL,node *r = NULL):value(value),l(l),r(r){};// 构造体内的有参构造函数和初始化列表 { 函数体个别为空} 
    // 无参构造函数初始化格局为 node():value(0),l(NULL),r(NULL);
};
// 树的创立函数
void build(int l,int r,int &t,node *&root){//& 代表传址而不是传值,因为 t 和指针 root 要在函数体内变动
    // l 代表传入的子数组的左下标,r 为相应的最右下标 
    int flag ;
    flag = -1;
    for(int i = l;i <= r;i++){if(in[i] == pre[t]){// 寻找与先序遍历中根节点字母值雷同的字母在中序遍历数组中的下标
            flag= i;// 记录该地位 
            break;// 找到后断掉执行上面的 
        }
    }
    if(flag == -1){// 递归进口:如果没找到,flag 还是 -1,阐明没有了间接 return 
        return; // 此部须要在 root 赋值之前,否组 in[-1] 报错 
    } 
    root = new node(in[flag]);//new 此处用法相当于 malloc, 找到了开拓一个指针地址并赋值为 in[flag] 对应的值
    t++;// 接着找第二个根节点在 in[] 中的地位
    if(flag > l){// 如果 flag = i = l; 阐明中序 in[] 以后根节点右边的数字曾经遍历完,无需再对右边子数组进行左递归 
        build(l,flag - 1,t,root->l);// 传入新的 t 和 root 持续递归右边子树 
    } 
    if(flag < r){// 如果 flag = i = r; 阐明中序 in[] 以后根节点左边的数字曾经遍历完,无需再对左边子数组进行右递归 
        build(flag + 1,r,t,root->r);// 传入新的 t 和 root 持续递归左边子树 
    } 
} 
void Post(node *root){// 二叉树后续遍历算法 
    if(root != NULL){Post(root->l);
        Post(root->r);
        ans[k++] = root->value;
    }
} 
int main(){
    int n = 0;
    while(cin >> n){for(int i = 1; i <= n; i++){cin >> pre[i]; 
        }
        for(int i = 1; i <= n; i++){cin >> in[i];
        }
        node *root;// 定义一个根节点
        int t;// 先序下标传参 t 
        t = 1;
        k = 0;
        build(1,n,t,root);// 传入参数创立树
        Post(root);
        for(int i = 0; i < k -1; i++){printf("%d",ans[i]);
        }
        printf("%d\n",ans[k - 1]);     
    }
    return 0;
} 

正文完
 0