Calculate the number of nodes in a binary tree, the number of leaf nodes, and the number of full nodes.

Source: Internet
Author: User

The following three items are written by me.

Static int node_count (const TREE tree) <br/>{< br/> int the_number_of_left_subtree = 0, the_number_of_right_subtree = 0, the_number_of_node = 0; </P> <p> If (tree! = NULL) <br/>{< br/> the_number_of_left_subtree = node_count (tree-> left); <br/> the_number_of_right_subtree = node_count (tree-> right ); <br/> the_number_of_node = the_number_of_left_subtree + the_number_of_right_subtree + 1; <br/>}</P> <p> return the_number_of_node; <br/>}</P> <p> static int leaf_count (const tree) <br/> {<br/> int the_number_of_left_subtree = 0, the_number_of_right_subtree = 0, the_number_of_leaf = 0; </P> <p> If (tree! = NULL) <br/>{< br/> the_number_of_left_subtree = leaf_count (tree-> left); <br/> the_number_of_right_subtree = leaf_count (tree-> right ); <br/> the_number_of_leaf = rows + rows; <br/> If (0 = rows & 0 = the_number_of_right_subtree) <br/> the_number_of_leaf ++; <br/>}</P> <p> return the_number_of_leaf; <br/>}</P> <p> static int full _ Count (const TREE tree) <br/>{< br/> int Limit = 0, the_number_of_right_subtree = 0, the_number_of_full_node = 0; </P> <p> If (tree! = NULL) <br/>{< br/> the_number_of_left_subtree = full_count (tree-> left); <br/> the_number_of_right_subtree = full_count (tree-> right ); <br/> If (the_number_of_left_subtree! = 0 & the_number_of_right_subtree! = 0) <br/> the_number_of_full_node + = the_number_of_left_subtree + the_number_of_right_subtree + 1; <br/> else if (tree-> left! = NULL & tree-> right! = NULL) <br/> the_number_of_full_node ++; <br/>}</P> <p> return the_number_of_full_node; <br/>}

The following three answers are provided, which is embarrassing.

Static int new_node_count (const TREE tree) <br/>{< br/> If (null = tree) <br/> return 0; <br/> return 1 + new_node_count (tree-> left) + new_node_count (tree-> right ); <br/>}</P> <p> static int new_leaf_count (const tree) <br/>{< br/> If (null = tree) <br/> return 0; <br/> else if (null = tree-> left & null = tree-> right) <br/> return 1; <br/> return new_leaf_count (tree-> left) + new_leaf _ Count (tree-> right); <br/>}</P> <p> static int new_full_count (const tree) <br/>{< br/> If (null = tree) <br/> return 0; <br/> return (tree-> left! = NULL & tree-> right! = NULL) + new_full_count (tree-> left) + new_full_count (tree-> right); <br/>}

Although I understand it, I can't write it by myself. To sum up some experience, this recursive function mostly uses return...

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.