PATA 1020 Tree Traversals

insert image description here
Inscription
Ideas
1. Create a structure of nodes
2. Recursively create a binary tree according to in-order traversal and post-order traversal. Assume that the current tree subscripts of in-order traversal are inL, inR, and the current tree subscripts of post-order traversal are poL, poR. The last one of the post-order traversal is the root node, the in-order traversal of the loop finds the root node, and the record is subscripted as k. It is numLeft=k-inL, so the subscript range of the left subtree of the current tree is [intL, K-1], the post-order traversal is [poL, poL+numLeft-1], and the right subtree is in order The traversal subscript range is [k+1, inR], and the post-order traversal subscript range is [poL+numLeft, poR].
3. Summary of the tree obtained by using the queue level traversal
:
1. The in-order sequence can be compared with the pre-order sequence, Any one of the post-order sequence and the hierarchical sequence can be used to construct a unique binary tree, and the latter three cannot be combined in two or three together to construct a unique binary tree. The reason is that the latter three all provide the root node, and the functions are the same, and they cannot distinguish the left and right subtrees.
2. The element of the queue used by BFS is the node pointer type. In fact, this question can only use the ordinary node type. Why use a pointer? Because some topics need to modify some values ​​of the node, the queue is only a copy of the original node, and modifying the elements in the queue does not change the original node. This point needs attention, it is best to keep It is customary to use pointers.

code show as below

#include <iostream>
#include <queue>
using namespace std;

const int Maxn=30+5;

struct node{
    
    
    int data;
    node* lchild;
    node* rchild;
};

//输入
int n;
int inorder[Maxn],postorder[Maxn];

node* create(int inL,int inR,int poL,int poR){
    
    
    //当inL==inR时是一个叶子结点还可以继续
    if(inL>inR)
        return NULL;
    node* root=new node;
    root->data=postorder[poR];
    int k;
    //找到中序遍历中的根节点,k保存中序遍历到根的下标
    for(k=inL;k<=inR;k++){
    
    
        if(inorder[k]==root->data)
            break;
    }
    //左子树的节点数
    int numLeft=k-inL;
    root->lchild=create(inL,k-1,poL,poL+numLeft-1);
    root->rchild=create(k+1,inR,poL+numLeft,poR-1);
    return root;
}

int num=0;//已经输出的结点数,为了避免最后多一个空格
void BFS(node* root){
    
    
    //注意元素类型是结点指针
    queue<node*> q;
    q.push(root);
    while(!q.empty()){
    
    
        node* now=q.front();
        q.pop();
        num++;
        if(num<n)
            printf("%d ",now->data);
        else
            printf("%d",now->data);
        if(now->lchild!=NULL)
            q.push(now->lchild);
        if(now->rchild!=NULL)
            q.push(now->rchild);
    }
}


int main()
{
    
    
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&postorder[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&inorder[i]);
    node* root=create(0,n-1,0,n-1);
    BFS(root);//层序遍历
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346955&siteId=291194637