22分求调QAQ
查看原帖
22分求调QAQ
1287951
xuruize666楼主2025/1/22 21:29

又是样例过的一天~~

#include <bits/stdc++.h>
using namespace std;

struct Node
{
    char data;
    Node *left, *right;
}*root;

string mid, pre;

Node *buildTree(string pre, string mid)
{
    char rootData = pre[0];
    int n = mid.find(rootData);
    Node *tmpTree = new Node;
    tmpTree -> data = rootData;
    if (pre.size() == 1)
        return tmpTree;

    if (n != -1)
    {
        tmpTree -> left = buildTree(pre.substr(1, n), mid.substr(0, n));
        tmpTree -> right = buildTree(pre.substr(n+1), mid.substr(n+1));
    }

    return tmpTree;
}

void postorder(Node *node) //后序遍历启动!!!
{
    if (node != nullptr)
    {
        postorder(node -> left);
        postorder(node -> right);
        cout << node -> data;
    }
}

int main()
{
    cin >> mid;
    cin >> pre;
    root = buildTree(pre, mid);

    postorder(root); //后序遍历

    return 0;
}

什么原因 22 分??且 RE + WA + 2AC?????求dalao调

提交结果

2025/1/22 21:29
加载中...