# 查找二叉树

【问题描述】

7

15

5 2 3

12 4 5

10 0 0

29 0 0

15 6 7

8 0 0

23 0 0

•‘

``` 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 struct node
6 {
7     int parent;
8     int date;
9     int lchild;
10     int rchild;
11     int wz;
12 }a[101];
13 int n;
14 int k;//待查找的值
15 int now=0;
16 void zx(int i)
17 {
18     /*if(a[i].date==k)
19     {
20         cout<<i;
21         return;
22     }
23     else
24     {*/
25         if(a[i].lchild!=0)
26         {
27             zx(a[i].lchild);
28         }
29         //else now++;
30         if(a[i].date==k)
31         {
32             cout<<now+1;
33             return;
34         }
35         else now++;
36         if(a[i].rchild!=0)
37         {
38             zx(a[i].rchild);
39         }
40         //else now++;
41
42     //}
43 }
44 int main()
45 {
46
47
48     cin>>n>>k;
49     //for(int i=1;i<=n;i++)
50     //a[i].wz=i;
51     for(int i=1;i<=n;i++)
52     {
53         cin>>a[i].date;
54         cin>>a[i].lchild;
55         a[a[i].lchild].parent=i;
56         cin>>a[i].rchild;
57         a[a[i].rchild].parent=i;
58     }
59     zx(1);
60     return 0;
61 }```

1811 篇文章123 人订阅

0 条评论