有根树的遍历和无根树的遍历

有根树的遍历

考察树的思想和dfs、bfs的实现,做不出来说明 dfs、bfs没有掌握 且 树的思想没有领悟。

//有根树的遍历 
#include <bits/stdc++.h>
using namespace std;
vector<int> to[200005];
int n, a, vis[200005], q[200005];
int cmp(int b, int c) { return b > c; }
void ad(int u, int v) { to[u].push_back(v); }
void dfs(int k) 
{
    cout << k << ' ';
    for (int i = 0; i < to[k].size(); i++) {
        if (vis[to[k][i]] == 0) {
            vis[to[k][i]] = 1;
            dfs(to[k][i]);
        }
    }
}
void bfs(int k) 
{
    int head = 1, tail = 1;
    q[tail++] = k;while (head < tail) {
        int now = q[head++];
        cout << now << " ";for (int i = 0; i < to[now].size(); i++) {
            if (vis[to[now][i]] == 0) {
                vis[to[now][i]] = 1;
                q[tail++] = to[now][i];
            }
        }
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n - 1; i++) {
        cin >> a;
        ad(a, i);
        ad(i, a);
    }
    for (int i = 0; i < n; i++) sort(to[i].begin(), to[i].end(), cmp);
    vis[0] = 1;
    dfs(0);
    memset(vis, 0, sizeof(vis));
    cout << endl;
    vis[0] = 1;
    bfs(0);
    return 0;
}

无根树的遍历

 这个题和上一个有什么区别?没区别 =.=

#include <bits/stdc++.h>
using namespace std;
vector<int> to[200005];
int n, a, vis[200005], q[200005], start;
int cmp(int b, int c) { return b > c; }
void ad(int u, int v) { to[u].push_back(v); }
void dfs(int k) 
{
    cout << k << ' ';
    for (int i = 0; i < to[k].size(); i++) {
        if (vis[to[k][i]] == 0) {
            vis[to[k][i]] = 1;
            dfs(to[k][i]);
        }
    }
}
void bfs(int k) {
    int head = 1, tail = 1;
    q[tail++] = k;
    while (head < tail) {
        int now = q[head++];
        cout << now << " ";
        for (int i = 0; i < to[now].size(); i++) {
            if (vis[to[now][i]] == 0) {
                vis[to[now][i]] = 1;
                q[tail++] = to[now][i];
            }
        }
    }
}
int main() 
{
    cin >> n;
    for (int i = 1; i <= n - 1; i++) {
        cin >> a;
        ad(a, i);
        ad(i, a);
    }
    cin >> start;
    for (int i = 0; i < n; i++) sort(to[i].begin(), to[i].end(), cmp);
    vis[start] = 1;
    dfs(start);
    memset(vis, 0, sizeof(vis));
    cout << endl;
    vis[start] = 1;
    bfs(start);
    return 0;
}
View Code

 

posted @ 2019-08-28 21:22  QUEKI嶺冬  阅读(802)  评论(0)    收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */