使用 DFS 计算树中给定级别的节点数
给定一个整数l和一棵树,表示为以顶点 0 为根的无向图。任务是打印l层存在的节点数。
例子:
Input: l = 2
Output: 4
我们已经讨论了 BFS 方法,在这篇文章中,我们将使用 DFS 解决它。
方法:想法是以DFS的方式遍历图。取两个变量, count和curr_level 。每当curr_level = l增加count的值时。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Class to represent a graph
class Graph {
// No. of vertices
int V;
// Pointer to an array containing
// adjacency lists
list* adj;
// A function used by NumOfNodes
void DFS(vector& visited, int src, int& curr_level,
int level, int& NumberOfNodes);
public:
// Constructor
Graph(int V);
// Function to add an edge to graph
void addEdge(int src, int des);
// Returns the no. of nodes
int NumOfNodes(int level);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list[V];
}
void Graph::addEdge(int src, int des)
{
adj[src].push_back(des);
adj[des].push_back(src);
}
// DFS function to keep track of
// number of nodes
void Graph::DFS(vector& visited, int src, int& curr_level,
int level, int& NumberOfNodes)
{
// Mark the current vertex as visited
visited[src] = true;
// If current level is equal
// to the given level, increment
// the no. of nodes
if (level == curr_level) {
NumberOfNodes++;
}
else if (level < curr_level)
return;
else {
list::iterator i;
// Recur for the vertices
// adjacent to the current vertex
for (i = adj[src].begin(); i != adj[src].end(); i++) {
if (!visited[*i]) {
curr_level++;
DFS(visited, *i, curr_level, level, NumberOfNodes);
}
}
}
curr_level--;
}
// Function to return the number of nodes
int Graph::NumOfNodes(int level)
{
// To keep track of current level
int curr_level = 0;
// For keeping track of visited
// nodes in DFS
vector visited(V, false);
// To store count of nodes at a
// given level
int NumberOfNodes = 0;
DFS(visited, 0, curr_level, level, NumberOfNodes);
return NumberOfNodes;
}
// Driver code
int main()
{
int V = 8;
Graph g(8);
g.addEdge(0, 1);
g.addEdge(0, 4);
g.addEdge(0, 7);
g.addEdge(4, 6);
g.addEdge(4, 5);
g.addEdge(4, 2);
g.addEdge(7, 3);
int level = 2;
cout << g.NumOfNodes(level);
return 0;
}
Python3
# Python3 implementation of the approach
# Class to represent a graph
class Graph:
def __init__(self, V):
# No. of vertices
self.V = V
# Pointer to an array containing
# adjacency lists
self.adj = [[] for i in range(self.V)]
def addEdge(self, src, des):
self.adj[src].append(des)
self.adj[des].append(src)
# DFS function to keep track of
# number of nodes
def DFS(self, visited, src, curr_level,
level, NumberOfNodes):
# Mark the current vertex as visited
visited[src] = True
# If current level is equal
# to the given level, increment
# the no. of nodes
if (level == curr_level):
NumberOfNodes += 1
elif (level < curr_level):
return
else:
# Recur for the vertices
# adjacent to the current vertex
for i in self.adj[src]:
if (not visited[i]):
curr_level += 1
curr_level, NumberOfNodes = self.DFS(
visited, i, curr_level,
level, NumberOfNodes)
curr_level -= 1
return curr_level, NumberOfNodes
# Function to return the number of nodes
def NumOfNodes(self, level):
# To keep track of current level
curr_level = 0
# For keeping track of visited
# nodes in DFS
visited = [False for i in range(self.V)]
# To store count of nodes at a
# given level
NumberOfNodes = 0
curr_level, NumberOfNodes = self.DFS(
visited, 0, curr_level,
level, NumberOfNodes)
return NumberOfNodes
# Driver code
if __name__=='__main__':
V = 8
g = Graph(8)
g.addEdge(0, 1)
g.addEdge(0, 4)
g.addEdge(0, 7)
g.addEdge(4, 6)
g.addEdge(4, 5)
g.addEdge(4, 2)
g.addEdge(7, 3)
level = 2
print(g.NumOfNodes(level))
# This code is contributed by pratham76
输出:
4
时间复杂度: O(N),其中 N 是图中节点的总数。
辅助空间: O(N)