Check whether a node is leaf node or not for multiple queries

Given a tree with N vertices numbered from 0 to N – 1 where 0 is the root node. The task is to check if a node is leaf node or not for multiple queries.

Examples:

Input:
       0
     /   \
   1      2
 /  \
3    4 
    /
  5
q[] = {0, 3, 4, 5}
Output:
No
Yes
No
Yes
From the graph, 2, 3 and 5 are the only leaf nodes.

Approach: Store the degree of all the vertices in an array degree[]. For each edge from A to B, degree[A] and degree[B] are incremented by 1. Now every node which not a root node and it has a degree of 1 is a leaf node and all the other nodes are not.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate the degree of all the vertices
void init(int degree[], vector<pair<int, int> > edges, int n)
{
    // Initializing degree of all the vertices as 0
    for (int i = 0; i < n; i++) {
        degree[i] = 0;
    }
  
    // For each edge from A to B, degree[A] and degree[B]
    // are increased by 1
    for (int i = 0; i < edges.size(); i++) {
        degree[edges[i].first]++;
        degree[edges[i].second]++;
    }
}
  
// Function to perform the queries
void performQueries(vector<pair<int, int> > edges,
                    vector<int> q, int n)
{
    // To store the of degree
    // of all the vertices
    int degree[n];
  
    // Calculate the degree for all the vertices
    init(degree, edges, n);
  
    // For every query
    for (int i = 0; i < q.size(); i++) {
  
        int node = q[i];
        if (node == 0) {
            cout << "No\n";
            continue;
        }
        // If the current node has 1 degree
        if (degree[node] == 1)
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
  
// Driver code
int main()
{
  
    // Number of vertices
    int n = 6;
  
    // Edges of the tree
    vector<pair<int, int> > edges = {
        { 0, 1 }, { 0, 2 }, { 1, 3 }, { 1, 4 }, { 4, 5 }
    };
  
    // Queries
    vector<int> q = { 0, 3, 4, 5 };
  
    // Perform the queries
    performQueries(edges, q, n);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to calculate the degree
# of all the vertices 
def init(degree, edges, n) : 
  
    # Initializing degree of
    # all the vertices as 0 
    for i in range(n) :
        degree[i] = 0
  
    # For each edge from A to B, 
    # degree[A] and degree[B] 
    # are increased by 1 
    for i in range(len(edges)) :
        degree[edges[i][0]] += 1
        degree[edges[i][1]] += 1
  
# Function to perform the queries 
def performQueries(edges, q, n) : 
  
    # To store the of degree 
    # of all the vertices 
    degree = [0] * n; 
  
    # Calculate the degree for all the vertices 
    init(degree, edges, n); 
  
    # For every query 
    for i in range(len(q)) :
  
        node = q[i]; 
        if (node == 0) :
            print("No"); 
            continue
  
        # If the current node has 1 degree 
        if (degree[node] == 1) :
            print("Yes"); 
        else :
            print("No"); 
  
# Driver code 
if __name__ == "__main__"
  
    # Number of vertices 
    n = 6
  
    # Edges of the tree 
    edges = [[ 0, 1 ], [ 0, 2 ], 
             [ 1, 3 ], [ 1, 4 ], 
             [ 4, 5 ]]; 
  
    # Queries 
    q = [ 0, 3, 4, 5 ]; 
  
    # Perform the queries 
    performQueries(edges, q, n); 
  
# This code is contributed by AnkitRai01

chevron_right


Output:

No
Yes
No
Yes

Time complexity: O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01