How to calculate the number of leaf nodes in a binary tree

Source: Internet
Author: User

Basic knowledge:

1. The binary tree layer I has a maximum of 2 ^ (i-1) nodes.

2. A k-depth binary tree can have at most 2 ^ K-1 nodes.

 

 

A complete binary tree has seven hundred nodes and asks how many leaf nodes the binary tree has.
According to "The layer I of a binary tree has at most 2 ^ (I & minus; 1) nodes, and the depth of k has at most 2 ^ k & minus; 1 node (the root node depth is 1) "this property:
Because 2 ^ 9-1 <700 <2 ^ 10-1, the depth of the complete binary tree is 10, and the first nine layers are full binary trees,
In this case, there are 2 ^ 9-1 = 511 nodes in the first nine layers, and the number of nodes in the ninth layer is 2 ^ (9-1) = 256.
Therefore, the number of leaf knots on the tenth layer is 700-511 = 189;
Now we can calculate the number of leaf nodes on the ninth layer.
Because the leaf node on the tenth layer is extended from the ninth layer, the node on the ninth layer should be removed. Because there are 189 in layer 10, remove (189 + 1)/2 = 95 in Layer 9;
Therefore, the number of leaf nodes on the ninth layer is 256-95 = 161. In addition, there are 189 leaf nodes on the tenth layer, and the final result is 350.

A complete binary tree with 800 nodes. What are the leaf nodes?

Answer: 400

How to calculate the number of leaf nodes in a binary tree

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.