E
E
Eugene2018-04-05 22:25:23
MySQL
Eugene, 2018-04-05 22:25:23

How to get the maximum depth of a tree?

Please help with requests. There is a group tree table: id, name, uplevel. Accordingly, the uplevel contains the id of the parent group. Let's say in group 1 there is 1 more nested subgroup, in the second the depth is 5, and in the third, only 2. How to find out that the maximum depth is 5?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-04-05
@Rsa97

At such representation of a tree - only to count. Get a list of id's of all groups that do not have a parent, then until the resulting list is empty, get a list of all groups whose parent groups are in the current list. Well, count how many of these levels are passed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question