简体   繁体   中英

Construct Binary Tree from in-order and post-order Traversals

I came across a problem that really interested me, but I am not sure I fully understand how to complete the task at hand: Design an algorithm to construct a binary tree from two n-long sequences, known to be the output of in-order and post-order traversals of the same binary tree.

I've managed to complete that much so far. Below is my (relevant) code so far, however I also would like to be able to identify sequences for which no binary tree exists. I'm not sure how to check for this. Can someone give me a nudge in the right direction?

node* build_tree(int in[], int inStart, int inEnd,
                 int post[], int postStart, int postEnd) {
    if(inStart > inEnd || postStart > postEnd)
        return NULL;

    int rootValue = post[postEnd];
    node *tNode = new_node(rootValue);

    // find the index of this node in in-order traversal
    int inIndex = search(in, inStart, inEnd, rootValue);

    // Using index in in-order traversal, construct left and right subtrees
    tNode->left = build_tree(in, inStart, inIndex-1, post, postStart, postStart+inIndex-(inStart+1));
    tNode->right = build_tree(in, inIndex+1, inEnd, post, postStart + inIndex - inStart, postEnd - 1);

    return tNode;
}

// Function to find index of value in arr[start...end]
// The function assumes that value is present in in[]
int search(int arr[], int start, int end, int value) {
    int i;
    for(i = start; i < end; i++) {
        if(arr[i] == value)
            return i;
    }

    return i;
}

// function that allocates a new node with the
// given data and NULL left and right pointers
node* new_node(int data) {
    node* n = (node*)malloc(sizeof(node));
    n->data = data;
    n->left = NULL;
    n->right = NULL;

    return n;
}

AFAIK a binary tree is not possible for the given sequences when ,

1) The two sequences are not of same length -- we can check the array length

2) The search of postorder value in inorder sequence fails -- search function should be modified to return negative value when search fails instead of returning i(after the for loop)

3) The given sequences do not represent postorder and inorder of the same tree -- as suggested by Gene traverse the tree and check the sequence

My Code for this problem :)

int searchelement( vector<int> &v , int start ,int end,int val)
{
    for(int i=start;i<=end;i++)
    {
    if(v[i]==val)
    return i;
    }
    return -1;
}
TreeNode * abc(vector<int> &postorder, vector<int> &inorder , int start ,int end , int &index)
{
    if(start>end)
    return NULL;
    int i = searchelement(inorder,start,end,postorder[index--]);
    TreeNode * temp = new TreeNode(inorder[i]);
    if(start==end)
    {
        return temp;
    }
    temp->right=abc(postorder,inorder,i+1,end,index);
    temp->left =abc(postorder,inorder,start,i-1,index);
    return temp;
}

main(){
    TreeNode * head =NULL;
    int start=0,end=inorder.size()-1,index=0;
    head= abc(preorder,inorder,start,end,index);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM