树中,求结点在树的第几层。如何不添加额外参数,修改成正确的代码
int Level(BTNode* b, char x)//求结点在第几层,从1开始
{
int t = 0;
if (b == NULL)
{
return 0;
}
else if (b->data == x)
{
return 1;
}
else
{
t = Level(b->lchild, x);
if (t != 0)
{
return t;
}
else
{
t = Level(b->rchild, x);
}
}
}
你代码里连+1都没有,不管递归多少次返回的不是0就是1,不相加的吗
if(t>0)
{
return t+1;
}
1 #include <stdio.h>
2 int main()
3 {
4 int a[10] = {2,4,6,8,12,14,15,23,29};
5 int i,j,temp;
6 printf ("Enter an integer:\n");
7 scanf ("%d",&temp);
8
9 if (temp<a[0]) // 如果比a[0]小,则原数组依次向后顺移,补充a[0]为输入的temp
10 {
11 for (i=8;i>=0;i--)
12 {
13 a[i+1] = a[i];
14 }
15 a[0] = temp;
16 }
17 else
18 {
19 for (i=8;i>=0;i--)
20 {
21 if (temp < a[i])
22 {
23 a[i+1] = a[i];
24 }
25 else
26 {
27 a[i+1] = temp;
28 break;
29 }
30 }
31 }
32 for (j=0;j<10;j++)
33 {
34 printf ("%5d",a[j]);
35 }
36 printf ("\n");
37 return 0;
38 }