1 solutions

  • 0
    @ 2026-11-18 18:59:42

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 100005;  // 开大一些
    struct Node {
        int l, r;
        char c;
    } tree[MAXN];
    int tot = 0, idx = 0;
    string s;
    
    int build() {
        if (idx >= (int)s.size()) return 0;  // 防止越界
        char ch = s[idx++];
        if (ch == '.') return 0;
        int u = ++tot;
        tree[u].c = ch;
        tree[u].l = build();
        tree[u].r = build();
        return u;
    }
    
    void dfs1(int u) {
        if (!u) return;
        dfs1(tree[u].l);
        cout << tree[u].c;
        dfs1(tree[u].r);
    }
    
    void dfs2(int u) {
        if (!u) return;
        dfs2(tree[u].l);
        dfs2(tree[u].r);
        cout << tree[u].c;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        cin >> s;
        int root = build();
        dfs1(root);
        cout << '\n';
        dfs2(root);
        cout << '\n';
        return 0;
    }
    
    • 1

    Information

    ID
    357
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    1
    Accepted
    1
    Uploaded By