Given the preorder and the inorder of a tree, output the postorder
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
Scoring
The shortest program in each language wins.
2 answers
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}
0 comment threads
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;}}
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;
}
}
0 comment threads