0

我正在制作给定前后顺序的二叉树形式。

预购:“vwbcyznamlp”

后订购:“cbznywmplav”

为此目的的逻辑

V W B C Y Z N A M L P
C B Z N Y W M P L A V

首先,注意根是 V,因为它既是前序中的第一个,也是后序中的最后一个:

V W B C Y Z N A M L P
  C B Z N Y W M P L A V

然后看W和A,它们分别是根的第一个左孩子和最后一个右孩子。前序中的 A 标志着遍历从根的左子树过渡到根的右子树的地方。后购中的 W 标记同一个地方。请注意,当您拆分遍历时,A 和 W 是相邻位置:

V W B C Y Z N    A M L P
  C B Z N Y W    M P L A V

现在你要为序列解决同样的问题:

W B C Y Z N    
C B Z N Y W

A M L P
M P L A

例如,第一个序列的下一步是:

W B C    Y Z N    
  C B    Z N Y W

我正在通过递归制作树,但递归中的一些条件我不认识。因此,当我通过 preoder 遍历这棵树时,答案不正确。

通过递归制作树后,答案是

预购:“vwbccyznaml”

我做的代码在这里

首先我制作了 Tree Struct

  #include <iostream>
  #include <string>
  #include <stack>
  using namespace std;
  struct TreeNode
  {
  public:
   string enteredData;
   struct TreeNode *left;
   struct TreeNode *right;
   struct TreeNode* newNode(string data)
    {
     struct TreeNode* temp = new TreeNode();
     temp->enteredData = data;
     temp->left = temp->right = NULL;
     return temp;
   }
  };
  struct TreeNode* Buildingtree(string preorder, string postorder);

附加功能

//for returning tree
struct TreeNode *constructTree(string preorder, string postorder)
{
int preIndex = 0;
return Buildingtree(preorder, postorder);
}


//for pre-order traversal
void printPreorder(struct TreeNode* node)
{
if (node == NULL)
    return;
cout << node->enteredData << " ";
printPreorder(node->left);
printPreorder(node->right);
}


//for building tree from given preorder and postorder
struct TreeNode* Buildingtree(string preorder, string postorder)
{
string newPreoderForFunction = "";
string newPostoderForFunction = "";
struct TreeNode* root = new TreeNode();
string rightpreorder;
string rightpostorder;
static stack<string> u;
int indexForPreOder = 1;
int indexForPostOrder = postorder.length() - 2;
if (indexForPostOrder <= -1)
{
    string g;
    if (preorder != "")
        g.assign(1, preorder[0]);
    else
        g.assign(1, postorder[0]);
    root = root->newNode(g);
    return root;
 }
 else
 {
    string findingCharFromPreorder;
    findingCharFromPreorder.assign(1, preorder[indexForPreOder]);
    string findingCharFromPostorder;
    findingCharFromPostorder.assign(1, postorder[indexForPostOrder]);
    size_t found;
    string h = "";
    if (preorder != "")
        h.assign(1, preorder[0]);
    else if (postorder != "")
        h.assign(1, postorder[0]);
    root = root->newNode(h);
    if ((found = preorder.find(findingCharFromPostorder)) != string::npos)
    {
        newPreoderForFunction = preorder.substr(1, found - 1);
        rightpreorder = preorder.substr(found, preorder.length() - 1);
        u.push(rightpreorder);
    }
    if ((found = postorder.find(findingCharFromPreorder)) != string::npos)
    {

        newPostoderForFunction = postorder.substr(0, found + 1);

        rightpostorder = postorder.substr(found + 1, indexForPostOrder);

        if (rightpostorder.length() > 1)
            rightpostorder.erase(rightpostorder.length() - 1);
        u.push(rightpostorder);

    }
  }
  root->left = Buildingtree(newPreoderForFunction, newPostoderForFunction);
  string a;
  string b;
  a = u.top();
  u.pop();
  b = u.top();
  u.pop();
  root->right = Buildingtree(b, a);
  return root;
}

主功能

int main()
{
string a1 = "vwbcyznamlp";
string a2 = "cbznywmplav";
struct TreeNode *root = constructTree(a1,a2);
printPreorder(root);
cout << endl;
system("pause");
}
4

0 回答 0