C#で指定されたIn OrderとPre-orderからポストオーダーを取得するには?
In Order: 8,4,10,9,11,2,5,1,6,5,7.
Pre-order: 1,2,4,8,9,10,11,5,3,6,7.
この注文と予約注文はテキストボックスから取得し、他のテキストボックスのボタンを押すと、注文後の結果を表示したいと思います。
私はすでに C++ で解決しましたが、PostOrder 関数に C# の問題があります。
int search(int arr[], int x, int n)
{
for (int i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
// Prints postorder traversal from given inorder and preorder traversals
void printPostOrder(int in[], int pre[], int n)
{
// The first element in pre[] is always root, search it
// in in[] to find left and right subtrees
int root = search(in, pre[0], n);
// If left subtree is not empty, print left subtree
if (root != 0)
printPostOrder(in, pre+1, root);
// If right subtree is not empty, print right subtree
if (root != n-1)
printPostOrder(in+root+1, pre+root+1, n-root-1);
// Print root
cout << pre[0] << " ";
}