1. Binary Tree definition
typedef struct BTREENODEELEMENT_T_ { void *data;} btreenodeelement_t;typedef struct Btreenode_t_ { btreenodeelement_t *m_pelemt; struct Btreenode_t_ *m_pleft; struct btreenode_t_ *m_pright;} btreenode_t;
2. Find the number of nodes in the K-layer of the binary tree
(1) Recursive method
Given root node Proot:
Returns 0 if Proot is empty, or if the number of layers Kthlevel <= 0, or if it is an empty tree or is not required;
If Proot is not empty and the layer kthlevel==1 at this point, then Proot is one of the K-tier nodes, then 1 is returned;
If Proot is not empty and the number of layers Kthlevel > 1, then the number of nodes in the Proot Zuozi (KthLevel-1) layer and Proot right subtree (KthLevel-1) layer nodes is required at this time;
int getbtreekthlevelnodestotal (btreenode_t *proot, int kthlevel) { if (proot = = NULL | | Kthlevel <= 0) return 0; if (proot! = NULL && Kthlevel = = 1) return 1; Return (Getbtreekthlevelnodestotal (Proot->m_pleft, KTHLEVEL-1) + getbtreekthlevelnodestotal (Proot->m_pright, KTHLEVEL-1));}
3, find the binary tree K-Layer leaf node points
(1) Recursive method
Given node Proot:
If Proot is empty, or if the number of layers Kthlevel <= 0, the empty tree or the number of layers is illegal, then 0 is returned;
If the proot is not empty, and if the number of layers is kthlevel==1, it is necessary to determine whether it is a leaf node:
If the Proot tree is empty, then the proot is one of the leaf nodes of the K-layer, then returns 1;
If one of the proot subtree is present, then the proot is not a leaf node, then 0 is returned;
If Proot is not empty, and the number of layers Kthlevel > 1, you need to return the Saozi right subtree node of the KTHLEVEL-1 layer.
int Getbtreekthlevelleafnodestotal (btreenode_t *proot, int kthlevel) { if (proot = = NULL | | Kthlevel <= 0) return 0; if (proot! = null && Kthlevel = = 1) { if (Proot->m_pleft = = NULL && Proot->m_pright = = null)
return 1; else return 0; } Return (Getbtreekthlevelleafnodestotal ( proot->m_pleft, KthLevel-1) + getbtreekthlevelleafnodestotal ( Proot->m_pright, KthLevel-1));}
Binary tree (8)----The number of nodes in the K-layer of the binary tree and the number of leaf nodes in the K-layer of the two-fork tree, the recursive method