Tree --dfs order

dfs order

Tree structure can be converted to a linear structure

So that each node in the subtree on a continuous interval, then we can use some of the data structures maintained tree linear

vector<int> v[maxn];

int in[maxn],out[maxn],idx;

void dfs(int u,int pre)
{
    in[u] = ++idx;
    for(auto i : v[u])
    {
        if(i == pre) continue;
        dfs(i,u);
    }
    out[u] = idx;
}

Guess you like

Origin www.cnblogs.com/hezongdnf/p/12591937.html