Binary tree (5)----Finding the number of binary tree nodes, recursive and non-recursive

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 binary tree node points

The number of nodes for any given subtree, the number of left subtree nodes + right subtree nodes +1, plus 1 is the root node itself


(1) Recursive mode:

Returns 0 if the given root node is null;

Returns if the given root node is not null: number of left dial hand tree nodes + right subtree node +1


int Getbtreenodestotal (btreenode_t *proot) {    if (proot = = NULL)        return 0;    Return (Getbtreenodestotal (proot->m_pleft) + getbtreenodestotal (proot->m_pright) + 1);}


(2) Non-recursive mode:

Any kind of traversal method: Pre-order, post-order, middle sequence, by layer traversal mode can be

Pre-order, post-order, middle-sequence, by-layer traversal.


































Binary tree (5)----Finding the number of binary tree nodes, recursive and non-recursive

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.