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 »
Sandbox

Given the preorder and the inorder of a tree, output the postorder [FINALIZED]

+3
−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]

Scoring

The shortest program in each language wins.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads