Description
The binary tree is stored in the form of a binary linked list, and a program is written to calculate the number of leaf nodes in the binary tree.
Input
Enter each node of the binary tree in the order of precedence, where # means cancel the establishment of the subtree node.
Output
Nodes that output the number of leaves in a binary tree
Sample Input ABD##EH###CF#I##G##
Sample Output 4
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<string.h> typedef struct node { char data; struct node*lchild; struct node*rchild; }BiTNode,*BiTree; void creat(BiTree*root) { char ch; ch=getchar(); if(ch=='#') { *root=NULL; } else{ *root=(BiTree)malloc(sizeof(BiTNode)); (*root)->data=ch; creat(&((*root)->lchild)); creat(&((*root)->rchild)); } } int num(BiTree root) { int number; if(root==NULL) { number=0; } else if((root->lchild==NULL)&&(root->rchild==NULL)) { number=1; } else {number=(num(root->lchild)+num(root->rchild));} return number; } intmain() { BiTree root; creat(&root); int n; n=num(root); printf("%d\n",n); return 0; }