1. An array of objects with tree structure
[{id:0},
{id:1,parentId:0},
{id:2},
{id:3,parentId:1},
{id:4,parentId:0},
{id:5,parentId:3}]
2. It is required to finish in
[{id:0},
{id:1,parentId:0},
{id:3,parentId:1},
{id:5,parentId:3}
{id:4,parentId:0},
{id:2}]
to explain: it is a depth-first traversal. Data with a parent node will have the attribute parentId. If a node has a child node (parentID== has its own id), it is directly inserted into his child node. Loop until the insert node has no child nodes. Those of the same class are arranged in previous order.
there are probably some ideas, but it is wrong to write for a long time. Ask God to give me a demo to learn
.