试图确定给定树中每个节点的级别

问题描述 投票:0回答:1

我正在尝试确定给定树中每个节点的级别。

我遇到以下错误:

bfs.java:3:错误:']'预期vectorv [10]; //用于维护上述邻接表的向量^bfs.java:4:错误:预期为“]”整数级[10]; //确定每个节点的级别^bfs.java:5:错误:预期为“]”bool vis [10]; //如果访问过,则标记该节点

class bfs {

    vector<int>v[10]; // Vector for maintaining adjacency list explained above
    int level[10]; // To determine the level of each node
    bool vis[10]; // Mark the node if visited
    void bfs(int s) {
        queue <int> q;
        q.push(s);
        level[ s ] = 0 ;  //Setting the level of the source node as 0
        vis[ s ] = true;
        while(!q.empty())
        {
            int p = q.front();
            q.pop();
            for(int i = 0;i < v[ p ].size() ; i++)
            {
                if(vis[ v[ p ][ i ] ] == false)
                {
                    //Setting the level of each node with an increment in the level of parent node
                    level[ v[ p ][ i ] ] = level[ p ]+1;
                    q.push(v[ p ][ i ]);
                    vis[ v[ p ][ i ] ] = true;
                }
            }
        }
    }
}
java breadth-first-search
1个回答
0
投票

您发布的代码有几个Java问题。其中之一是类以大写字母开头(例如,Vector和Queue)。要回答有关错误的特定问题,这里是一个起点。

Vector<Integer>[] v = new Vector[10]; // Vector for maintaining adjacency list explained above
int[] level = new int[10]; // To determine the level of each node
boolean[] vis = new boolean[10]; // Mark the node if visited
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.