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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.