Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Given the preorder and the inorder of a tree, output the postorder

+5
−0

Definitions

A binary tree is either a null (leaf), or an object (node). A node contains a value (non-negative integer) and two pointers (left and right) to two separate binary trees.

A binary tree can be traversed in many different ways. Here we define preorder, inorder and postorder. We define them recursively using a pseudocode:

preorder(tree):
  if tree is not a leaf:
    visit(tree.value)
    preorder(tree.left)
    preorder(tree.right)

inorder(tree):
  if tree is not a leaf:
    inorder(tree.left)
    visit(tree.value)
    inorder(tree.right)

postorder(tree):
  if tree is not a leaf:
    postorder(tree.left)
    postorder(tree.right)
    visit(tree.value)

Each algorithm sequentially visits all nodes of the given tree.

Task

Given the sequence of nodes that is obtained by applying the preorder algorithm to a binary tree and given the sequence of nodes that is obtained by applying the inorder algorithm to the same tree, output the sequence of nodes that would be obtained by applying the postorder algorithm to the same tree.

Input

Two arrays of integers. The first one corresponds to the preorder and the second one corresponds to the inorder. Both arrays contain the same number of elements. Each element in an array is unique (there are no two distinct nodes with the same value).

Output

A single array of integers corresponding to the postorder.

Test cases

Input:  [[], []]
Output: []

Input:  [[0], [0]]
Output: [0]

Input:  [[0, 1], [1, 0]]
Output: [1, 0]

Input:  [[0, 1], [0, 1]]
Output: [1, 0]

Input:  [[0, 1, 2],
         [1, 0, 2]]
Output: [1, 2, 0]

Input:  [[0, 1, 2, 3, 4],
         [1, 2, 0, 4, 3]]
Output: [2, 1, 4, 3, 0]

Input:  [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
         [4, 3, 2, 1, 7, 6, 8, 5, 9, 10, 0, 12, 14, 13, 15, 11, 18, 17, 16, 19, 20]]
Output: [4, 3, 2, 7, 8, 6, 10, 9, 5, 1, 14, 15, 13, 12, 18, 17, 20, 19, 16, 11, 0]

Reference implementation

Try it online!

Scoring

The shortest program in each language wins.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+2
−0

C (gcc), 114 bytes

f(int n,int*p,int*i,int**o){if(n){int*m=i,k;for(;*m!=*p;++m);k=m-i;f(k,p+1,i,o);f(n-k-1,p+k+1,m+1,o);*(*o)++=*p;}}

Try it online!

Arguments:

  • n is the length of the arrays
  • p is the preorder array
  • i is the inorder array
  • o is a pointer to a pointer to where the postorder array is to be stored.

Here's an ungolfed and commented version:

f(int size, int *preorder, int *inorder, int **output)
{
  if (size) /* if the arrays are zero length, do nothing */
  {
    /* The first element of the preorder array is the root.
       Find it in the inorder array. */
    int *m = inorder, k;
    for(;*m != *preorder; ++m);

    /* everything preceding the root in the inorder list
       is in the left subtree; store its length in k */
    k = m - inorder;

    /* recursively call the function for the left and right
       subtree */
    f(k, preorder + 1, i, output);
    f(size - k - 1, preorder + k + 1, m + 1, output);

    /* finally, append the value of the root node */
    *(*output)++ = *preorder;
  }
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Ruby, 75 72 bytes

f=->((e,*r),n){(i=n.index e)?f[r[0,i],n[...i]]+f[r[i..],n[i+1..]]+[e]:n}

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »