[DONGLE] [Data Structure] [Algorithm] Search the node and the number of leaf nodes

Recently, through doing data structure test questions, a lot of trees have appeared The number of nodes. Here is a summary of the algorithm for finding nodes:


Known one Lesson degree iskThe tree hasn1one degree is1 strong>‘s node,n2< span lang="zh-CN">the degree is2 Node,n3one degree< /span>3’s node,,nk< /span>A node with a degree ofk . Find the number of summary points and leaf nodes (degree is 0)

Suppose there are N nodes and N-1 edges ( BecauseThe relationship between the edges and nodes in the treeis:number of nodes=number of sides+1) , X leaf nodes, there are:

N=X+1+2+3+…+k< /span> /span>(It must be a degree of existence)

N-1=0*X+1*n1+2*n2+3*n3+…+k*nk

X=(1*n1+2*n2+3*n3+…+k*nk)(1+2+3+…+k)

I would like to help you~

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5000 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.