1 solutions

  • 0
    @ 2025-11-5 16:46:00

    C++ :

    #include <iostream>
    using namespace std;
    typedef struct node{
    	char x;
    	int  l;
    	int  r;
    }node ;
    void preOrder(node * t,int n){
    	cout<<t[n].x;
    	if(t[n].l==0&&t[n].r==0){
    		return ;
    	} 
    	if(t[n].l!=0)preOrder(t,t[n].l);
    	if(t[n].r!=0)preOrder(t,t[n].r);
    	return ;
    }
    void inOrder(node * t,int n){
    	
    	if(t[n].l!=0)inOrder(t,t[n].l);
    	cout<<t[n].x;
    	if(t[n].l==0&&t[n].r==0){
    		return ;
    	} 
    	if(t[n].r!=0)inOrder(t,t[n].r);
    	return ;
    }
    void postOrder(node * t,int n){
    	
    	if(t[n].l!=0)postOrder(t,t[n].l);
    	
    	if(t[n].r!=0)postOrder(t,t[n].r);
    	cout<<t[n].x;
    	if(t[n].l==0&&t[n].r==0){
    		return ;
    	} 
    	return ;
    }
    int main(){
    	int n;
    	cin>>n;
    	node * t = new node [n+1];
    	for(int i=1;i<=n;i++){
    		cin>>t[i].x>>t[i].l>>t[i].r;
    	}
    	
    	//----------------ÏÈÐò±éÀú--------------- 
    	preOrder(t,1);cout<<endl;
    	//----------------ÖÐÐò±éÀú---------------
    	inOrder(t,1);cout<<endl;
    	//----------------ºóÐò±éÀú---------------
    	postOrder(t,1);cout<<endl;
    
    	return 0;
    }
    
    • 1

    Information

    ID
    17974
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By