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~