1 条题解

  • 0
    @ 2025-10-10 20:11:59

    C :

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int  d[200],l[200],r[200];
    int n,m,t;
    int tree(int k) {
    	if (d[k]==0) return 0;
    		else {
    			tree(l[k]);
    			t++;
    			if (d[k]==m) printf("%d\n",t);
    			tree(r[k]);
    		}
    	return 0;
    }
    
    int main(){
    	int i;
    	scanf("%d%d",&n,&m);
    	for (i=1;i<=n;i++) scanf("%d%d%d",&d[i],&l[i],&r[i]);
        t=0;
    	tree(1);
    	return 0;
    }
    
    

    C++ :

    #include <iostream>
    #include <vector>
    #include <map>
    #include <set>
    #include <string>
    #include <algorithm>
    #include <math.h>
    #include <string.h>
    #include <stack> 
    
    using namespace std;
    
    
    struct Node{
    	Node *lchild;
    	Node *rchild;
    	int value;
    }; 
    
    void creatTree(Node *root){
    	root->lchild=NULL;
    	root->rchild=NULL;
    	root->value=-1;
    }
    
    struct treeNode{
    	int lc;
    	int rc;
    	int value;
    };
    
    vector<treeNode> vec;
    vector<int> vec2;
    
    
    void BTree(int i){
    	if(vec[i].lc!=0)
    		BTree(vec[i].lc);
    	int a=vec[i].value; 
    	vec2.insert(vec2.end(),a);
    	if(vec[i].rc!=0)
    		BTree(vec[i].rc);
    	return ;
    }
    
    int main(){
    	
    	int n;
    	int Num;
    	scanf("%d%d",&n,&Num);
    	treeNode treeNull;
    	vec.insert(vec.end(),treeNull);
    	for(int i=0;i<n;i++){
    		treeNode treeTemp;
    		scanf("%d%d%d",&treeTemp.value,&treeTemp.lc,&treeTemp.rc);
    		vec.insert(vec.end(),treeTemp);
    	}
    	BTree(1); 
    	int count=-1;
    	for(int i=0;i<vec2.size();i++){
    		if(vec2[i]==Num){
    			count=i;
    			break;
    		}
    	}
    	if(count!=-1){
    		cout<<count+1<<endl;
    	} 
    	return 0;
    
    }
    
    
    
    
    
    
    
    
    
    • 1

    信息

    ID
    1086
    时间
    1000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者