http://ybt.ssoier.cn:8088/problem_show.php?pid=1367
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n, x; 4 int ans; 5 struct node{ 6 int data, lchild, rchild; 7 }; 8 node tree[1024]; 9 int d[1024]; 10 int root; 11 void zxprint(int root)//中序遍历这棵树 12 { 13 if(tree[root].lchild!=0) 14 zxprint(tree[root].lchild); 15 ans++; 16 if(tree[root].data==x) 17 cout<<ans; 18 if(tree[root].rchild!=0) 19 zxprint(tree[root].rchild); 20 } 21 int main() 22 { 23 cin>>n>>x; 24 for(int i=1; i<=n; i++){ 25 cin>>tree[i].data>>tree[i].lchild>>tree[i].rchild; 26 d[tree[i].lchild]=d[tree[i].rchild]=1;//统度 27 } 28 //for(int i=1; i<=n; i++)cout<<i<<":"<<tree[i].data<<" "<<tree[i].lchild<<" "<<tree[i].rchild<<" "<<endl; 29 for(int i=1; i<=n; i++)//找根的下标 30 if(!d[i])root=i; 31 //cout<<root; 32 zxprint(root); 33 return 0; 34 }