using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static string answer = "";
static void Main(string[] args)
{
string preord = "12345678";
string inord = "32541876";
former(preord, inord, false);
while(s.Count>0)
{
answer = answer + s.Pop();
}
Console.WriteLine(answer);
}
static Stack s = new Stack();
static void former(string preord, string inord, bool control)
{
if (inord.Length == 1 || inord.Length < 1)
{
if (inord.Length == 1)
{
answer = answer + inord;
if (control == true)
{
answer = answer + s.Pop();
}
return;
}
else
{
if (control == true)
{
answer = answer + s.Pop();
}
return;
}
}
else
{
int i=0, j=0;
for (i = 0; i < preord.Length; i++)
{
bool flag = false;
for (j = 0; j < inord.Length; j++)
{
if (preord[i] == inord[j])
{
flag = true;
break;
}
}
if(flag == true)
{break;}
}
string s3 = inord;
string s4 = preord;
//Left sub-tree in the inorder traversal.
string s2 = inord.Remove(j, inord.Length - j);
//Left sub-tree in the preorder traversal.
string s1 = preord.Substring(i + 1, s2.Length);
//Right sub-tree in the inorder traversal.
string s5 = s3.Remove(0, j + 1);
int index = s4.IndexOf(s1, 0);
//Right sub-tree in the preorder traversal.
string s6 = s4.Remove(index, s1.Length);
//Push the root into the stack.
s.Push(s6[0]);
//Right sub-tree without the root in the preorder traversal.
s6 = s6.Remove(0, 1);
control = false;
former(s1, s2, control);
control = true;
former(s6, s5, control);
}
}
}
}