Data Structure Tree exercises (4)

Source: Internet
Author: User

 

A binary tree is represented by a binary linked list to determine the number of leaf nodes on a layer K.

Basic Idea: uses the queue structure to traverse layers, and records the number of leaf nodes when traversing the K-layer.

Code:

 

Int leafklevel (bitree BT, int K) {If (bt = NULL | K <1) {return 0;} bitree P = BT, Q []; int front = 0, rear = 1, leaf = 0; int last = 1, level = 1; Q [1] = P; while (front <= rear) {P = Q [++ front]; If (Level = K &&! P-> lchild &&! P-> rchild) {leaf ++;} If (p-> lchild) {q [++ rear] = p-> lchild;} If (p-> rchild) {q [++ rear] = p-> rchild;} If (front = last) {level ++; last = rear;} If (level> K) {return leaf ;}}}

 

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.