1 條題解

  • 2
    @ 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;
    }
    
    

    資訊

    ID
    262
    時間
    1000ms
    記憶體
    256MiB
    難度
    5
    標籤
    遞交數
    32
    已透過
    7
    上傳者