Code Submission Evaluation System Login

CSES Problem Set

Tree Traversals

Task | Statistics

CSES - Tree TraversalsCSES - Tree Traversals

Time limit:1.00 s Memory limit:512 MB

There are three common ways to traverse the nodes of a binary tree:
There is a binary tree of $n$ nodes with distinct labels. You are given the preorder and inorder traversals of the tree, and your task is to determine its postorder traversal.


The first input line has an integer $n$: the number of nodes. The nodes are numbered $1,2,\dots,n$.

After this, there are two lines describing the preorder and inorder traversals of the tree. Both lines consist of $n$ integers.

You can assume that the input corresponds to a binary tree.


Print the postorder traversal of the tree.


5 3 2 1 4
3 5 1 2 4

3 1 4 2 5