如何在 C# 中从给定的 InOrder 和 PreOrder 获取 postOrder?
How to get postOrder from given InOrder and PreOrder in C#?
如何在 C# 中从给定的 In Order 和 Pre-order 中获取 post 订单?
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.
这是订单和预购我从文本框中获取它,当在其他文本框中按下按钮时我想显示 Post 订单结果。
我已经用 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] << " ";
}
试试这个
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() + " ");
}
如何在 C# 中从给定的 In Order 和 Pre-order 中获取 post 订单?
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.
这是订单和预购我从文本框中获取它,当在其他文本框中按下按钮时我想显示 Post 订单结果。
我已经用 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] << " ";
}
试试这个
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() + " ");
}