Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Reconstruction of a Tree

Write a program which reads two sequences of nodes obtained by the preorder tree walk and the inorder tree walk on a binary tree respectively, and prints a sequence of the nodes obtained by the postorder tree walk on the binary tree.

Input

In the first line, an integer $n$, which is the number of nodes in the binary tree, is given.

In the second line, the sequence of node IDs obtained by the preorder tree walk is given separated by space characters.

In the second line, the sequence of node IDs obtained by the inorder tree walk is given separated by space characters.

Every node has a unique ID from $1$ to $n$. Note that the root does not always correspond to $1$.

Output

Print the sequence of node IDs obtained by the postorder tree walk in a line. Put a single space character between adjacent IDs.

Constraints

$1 \leq n \leq 40$

Sample Input 1

5
1 2 3 4 5
3 2 4 1 5

Sample Output 1

3 4 2 5 1

Sample Input 2

4
1 2 3 4
1 2 3 4

Sample Output 2

4 3 2 1

Solution in PHP (judge verdict: Accepted)

<?php
solve1();
// walk();

// 自力解
function solve1(){
	$n = (int)trim(fgets(STDIN));

	$list = [];
	for ($i=0; $i < $n; $i++) { 
		$node = new BTreeNode($i+1);
		$list[$node->getId()] = $node;
	}
	$preorder = [];
	$input = explode(' ', trim(fgets(STDIN)));
	for ($i=0; $i < $n; $i++) { 
		$preorder[] = (int)$input[$i];
	}
	$inorder = [];
	$input = explode(' ', trim(fgets(STDIN)));
	for ($i=0; $i < $n; $i++) { 
		$inorder[] = (int)$input[$i];
	}
	$end = $n;
	dfs($preorder, $inorder, $list, $preorder[0]);
	$res = [];
	$res = postorderWalk($list, $preorder[0], $res);
	echo implode(' ', $res) . PHP_EOL;
}

function dfs($preorder, $inorder, $list, $p){
	$children = getChildren($inorder, $p);
	if (count($children['left']) === 1) {
		$list[$p]->setLeft($children['left'][0]);
		$list[$children['left'][0]]->setParentId($p);
	}elseif(count($children['left']) > 1){
		$p_left = null;
		foreach ($preorder as $pre) {
			if (in_array($pre, $children['left'])) {
				$p_left = $pre;
				break;
			}
		}
		$list[$p]->setLeft($p_left);
		$list[$p_left]->setParentId($p);
		dfs($preorder, $children['left'], $list, $p_left);
	}

	if (count($children['right']) === 1) {
		$list[$p]->setRight($children['right'][0]);
		$list[$children['right'][0]]->setParentId($p);
	}elseif(count($children['right']) > 1){
		$p_right = null;
		foreach ($preorder as $pre) {
			if (in_array($pre, $children['right'])) {
				$p_right = $pre;
				break;
			}
		}
		$list[$p]->setRight($p_right);
		$list[$p_right]->setParentId($p);
		dfs($preorder, $children['right'], $list, $p_right);
	}
}

function getChildren($inorder, $p){
	$n = count($inorder);
	$left = [];
	$right = [];
	$r_flg = false;
	for ($i=0; $i < $n; $i++) { 
		if ($inorder[$i] === $p) {
			$r_flg = true;
		}else{
			if ($r_flg) {
				$right[] = $inorder[$i];
			}else{
				$left[] = $inorder[$i];
			}
		}
	}
	return [
		'left' => $left,
		'right' => $right,
	];
}
// function dfs($list, $id, $depth){
// 	$list[$id]->setDepth($depth);
// 	++$depth;
// 	$height = 0;
// 	if($list[$id]->hasChild()){
// 		$children = $list[$id]->getChildren();
// 		foreach ($children as $c) {
// 			$h = dfs($list, $c, $depth);
// 			$height = max($height, $h);
// 		}
// 	}
// 	$list[$id]->setHeight($height);
// 	return ++$height;
// }

// function walk(){
// 	$n = (int)trim(fgets(STDIN));

// 	$list = [];
// 	for ($i=0; $i < $n; $i++) { 
// 		$node = new BTreeNode($i);
// 		$list[$node->getId()] = $node;
// 	}
// 	for ($i=0; $i < $n; $i++) { 
// 		$input = explode(' ', trim(fgets(STDIN)));
// 		$id = (int)$input[0];
// 		$list[$id]->setLeft((int)$input[1]);
// 		if (isset($list[(int)$input[1]])) {
// 			$list[(int)$input[1]]->setParentId($id);
// 		}
// 		$list[$id]->setRight((int)$input[2]);
// 		if (isset($list[(int)$input[2]])) {
// 			$list[(int)$input[2]]->setParentId($id);
// 		}
// 	}
// 	for ($i=0; $i < $n; $i++) { 
// 		if ($list[$i]->getParentId() === -1) {
// 			// root
// 			$curId = $i;
// 			break;
// 		}
// 	}
// 	echo 'Preorder' . PHP_EOL;
// 	preorderWalk($list, $curId);
// 	echo PHP_EOL;

// 	echo 'Inorder' . PHP_EOL;
// 	inorderWalk($list, $curId);
// 	echo PHP_EOL;

// 	echo 'Postorder' . PHP_EOL;
// 	postorderWalk($list, $curId);
// 	echo PHP_EOL;

// }


function preorderWalk($list, $id){
	echo ' ' . $id;
	if ($list[$id]->hasLeft()) {
		preorderWalk($list, $list[$id]->getLeft());
	}
	if ($list[$id]->hasRight()) {
		preorderWalk($list, $list[$id]->getRight());
	}
}

function inorderWalk($list, $id){
	if ($list[$id]->hasLeft()) {
		inorderWalk($list, $list[$id]->getLeft());
	}
	echo ' ' . $id;
	if ($list[$id]->hasRight()) {
		inorderWalk($list, $list[$id]->getRight());
	}
}
function postorderWalk($list, $id, $result){
	if ($list[$id]->hasLeft()) {
		$result = postorderWalk($list, $list[$id]->getLeft(), $result);
	}
	if ($list[$id]->hasRight()) {
		$result = postorderWalk($list, $list[$id]->getRight(), $result);
	}
	$result[] = $id;
	return $result;
}

class BTreeNode
{
	private $id;
	private $depth = 0;
	private $height = 0;
	private $parent_id = -1;
	private $left = -1;
	private $right = -1;

	function __construct($id)
	{
		$this->id = $id;
	}
	function setParentId($p_id){
		$this->parent_id = $p_id;
	}
	function setLeft($l_id){
		$this->left = $l_id;
	}
	function setRight($r_id){
		$this->right = $r_id;
	}
	function getId(){
		return $this->id;
	}
	function getParentId(){
		return $this->parent_id;
	}
	function getLeft(){
		return $this->left;
	}
	function getRight(){
		return $this->right;
	}
	function hasChild(){
		return ($this->left !== -1) || ($this->right !== -1);
	}
	function getChildren(){
		$children = [];
		if ($this->hasLeft()) {
			$children[] = $this->left;
		}
		if ($this->hasRight()) {
			$children[] = $this->right;
		}
		return $children;
	}
	function getAnotherChild($c_id){
		$c = -1;
		if ($this->getLeft() === $c_id) {
			$c = $this->getRight();
		}elseif ($this->getRight() === $c_id) {
			$c = $this->getLeft();
		}
		return $c;
	}
	function hasLeft(){
		return $this->left !== -1;
	}
	function hasRight(){
		return $this->right !== -1;
	}
	function setDepth($d){
		$this->depth = $d;
	}
	function addDepth(){
		$this->depth += 1;
	}
	function getDepth(){
		return $this->depth;
	}
	function setHeight($h){
		$this->height = $h;
	}
	function getHeight(){
		return $this->height;
	}
}