Home > other >  Use the method of traversing binary tree counting the number of a binary tree leaves node, to return
Use the method of traversing binary tree counting the number of a binary tree leaves node, to return

Time:09-18

The complete code is as follows:
# include & lt; stdio.h>
# include & lt; The malloc. H>
# include & lt; Conio. H>

Typedef char DataType.

Typedef struct Node
{
DataType data;
Struct Node * LChild;
Struct Node * RChild;
} BiTNode, * BiTree;


Void CreateBiTree (bt) BiTree *
{
Char ch;
Ch=getchar ();
If bt (ch=='#') *=NULL;
The else
{
* bt=(BiTree) malloc (sizeof (BiTNode));//create a new node
Bt (*) - & gt; data=https://bbs.csdn.net/topics/ch;
CreateBiTree (& amp; (bt) (* - & gt; LChild));//generated left subtree
CreateBiTree (& amp; (bt) (* - & gt; RChild));//generated right subtree
}
}
Int count (BiTNode * p)
{
int a,b,c;
If (p==NULL)
return 0;
The else
{
A=count (p - & gt; LChild);
B=count (p - & gt; RChild);
}
The return of a + b + 1;
}
Void countleaf (BiTNode * p)
{
int count=0;
If (p!=NULL)
{
Countleaf (p - & gt; LChild);
Countleaf (p - & gt; RChild);
If (p - & gt; LChild==NULL& & P - & gt; RChild==NULL)
{
count=count+1;
}
}
Return the count.
}
Int main ()
{
BiTree T;
Printf (" \ n nodes number: \ n ");
Count (T);
Printf (" \ n number of leaf nodes: \ n ");
Countleaf (T);
Printf (" \ n % d ");
}

CodePudding user response:

The return value is of type int, you write is of type void
  • Related