2 solutions

  • 1
    @ 2025-12-20 10:15:10
    #include<bits/stdc++.h>
    using namespace std;
    string s1,s2;
    void dfs(int l1,int r1,int l2,int r2)
    {
        int m=s2.find(s1[l1]);//在中序遍历中找到根的位置 
        if(m>l2) dfs(l1+1,l1+m-l2,l2,m-1); //存在左子树 
        if(m<r2) dfs(l1+m-l2+1,r1,m+1,r2); //存在右子树 
        cout<<s1[l1];  //输出根节点 
    }
    int main()
    {
        cin>>s1>>s2; //先序,中序 
        dfs(0,s1.size()-1,0,s2.size()-1);
        return 0;
    }
    
    

    Information

    ID
    263
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    22
    Accepted
    9
    Uploaded By