如何从C#中的给定InOrder和PreOrder获取postOrder?

时间:2016-12-17 14:50:55

标签: c# inorder preorder postorder

如何从C#中的给定订单和预购订单中获得订单?

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] << " ";
}

1 个答案:

答案 0 :(得分:0)

试试这个

        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.Skip(1).ToArray(), root);

           // If right subtree is not empty, print right subtree
           if (root != n-1)
              printPostOrder(_in.Skip(root+1).ToArray(), pre.Skip(root+1).ToArray(), n-root-1);

           // Print root
           Console.Write(pre[0].ToString() + " ");
         }