1 solutions

  • 1
    @ 2025-8-1 10:16:27
    #include<bits/stdc++.h>
    using namespace std;
    const int N=100;
    int idx;
    struct Node{
    	int l,r;
    	char v;
    }tr[N];
    int build()
    {
    	char ch;
    	cin>>ch;
    	if(ch=='.') return -1;
    	int bt=++idx;
    	tr[bt].v=ch;
    	tr[bt].l=build();
    	tr[bt].r=build();
    	return bt;	
    } 
    void inorder(int root)
    {
    	if(tr[root].l!=-1) inorder(tr[root].l);
    	cout<<tr[root].v;
    	if(tr[root].r!=-1) inorder(tr[root].r);
    }
    void postorder(int root)
    {
    	if(tr[root].l!=-1) postorder(tr[root].l);
    	if(tr[root].r!=-1) postorder(tr[root].r);
    	cout<<tr[root].v;
    }
    int main()
    {
    	memset(tr,-1,sizeof tr);
    	build();
    	inorder(1);
    	cout<<endl;
    	postorder(1);
    	return 0;
    }
    
    

    Information

    ID
    262
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    31
    Accepted
    6
    Uploaded By