Suppose my family tree is like this:
After converting to the representation of children and brothers, it looks like this:
? What we need to do is: at this time, we need to find out how many generations there are and finally come out as a generation.
? If algebra is the height of the tree according to the first picture, then the last generation is the last layer of the tree, but the binary linked list method is not as intuitive as the first picture, but it is still very clear as long as the essence of the binary linked list method is grasped. According to the characteristics of child brother representation (see the schematic diagram of binary linked list method), the left subtree of node 3 stores its child nodes, and the right subtree of node 3 stores its cousin nodes (see the first schematic diagram). Assuming that each of our nodes has a variable to store which generation it is, starting with node 1, if we want to find out which generation node 10 is, we should do this: node 1 is the first generation, then passing through node 5 is the second generation, and then we see that node 10 is the third generation. Because the left subtree of the ith node is his child, since it is a child, the algebra must be+1, and the right subtree is the same generation as the ith node (cousin), so 1 cannot be added. Essentially, it is algebra+1 to the left and algebra to the right. This is the train of thought of this topic. Through this method, we can know how many generations each node has saved algebraic information (saved by variables). Just traverse the tree again and output the last generation of nodes. Is that clear? I started writing programs when I was clear.