Count of leaf nodes of the tree whose weighted string is a palindrome

Given an N-ary tree, and the weights which are in the form of strings of all the nodes, the task is to count the number of leaf nodes whose weights are palindrome.

Examples:

Input: 
               1(ab)
              /  \
       (abca)2     5 (aba)
          /   \ 
   (axxa)3     4 (geeks)
Output: 2
Explanation: 
Only the weights of the leaf nodes
"axxa" and "aba" are palindromes.

Input: 
               1(abx)
              /  
            2(abaa) 
           /    
          3(amma)  
Output: 1
Explanation: 
Only the weight of the leaf
node "amma" is palindrome.

Approach: To solve the problem mentioned above follow the steps given below:

  • Depth First Search can be used to traverse the complete tree.
  • We will keep track of parent while traversing to avoid the visited node array.
  • Initially for every node we can set a flag and if the node have at least one child (i.e. non-leaf node) then we will reset the flag.
  • The nodes with no children are the leaf nodes. For every leaf node, we will check if it’s string is palindrome or not. If yes then increment the count.

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;
  
int cnt = 0;
  
vector<int> graph[100];
vector<string> weight(100);
  
// Function that returns true
// if x is a palindrome
bool isPalindrome(string x)
{
    int n = x.size();
    for (int i = 0; i < n / 2; i++) {
        if (x[i] != x[n - 1 - i])
            return false;
    }
    return true;
}
  
// Function to perform DFS on the tree
void dfs(int node, int parent)
{
    int flag = 1;
  
    // Iterating the children of current node
    for (int to : graph[node]) {
  
        // There is at least a child
        // of the current node
        if (to == parent)
            continue;
        flag = 0;
        dfs(to, node);
    }
  
    // Current node is connected to only
    // its parent i.e. it is a leaf node
    if (flag == 1) {
        // Weight of the current node
        string x = weight[node];
  
        // If the weight is a palindrome
        if (isPalindrome(x))
            cnt += 1;
    }
}
  
// Driver code
int main()
{
  
    // Weights of the node
    weight[1] = "ab";
    weight[2] = "abca";
    weight[3] = "axxa";
    weight[4] = "geeks";
    weight[5] = "aba";
  
    // Edges of the tree
    graph[1].push_back(2);
    graph[2].push_back(3);
    graph[2].push_back(4);
    graph[1].push_back(5);
  
    dfs(1, 1);
  
    cout << cnt;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
class GFG{
  
static int cnt = 0;
  
static Vector<Integer> []graph = new Vector[100];
static String []weight = new String[100];
  
// Function that returns true
// if x is a palindrome
static boolean isPalindrome(String x)
{
    int n = x.length();
    for (int i = 0; i < n / 2; i++) 
    {
        if (x.charAt(i) != x.charAt(n - 1 - i))
            return false;
    }
    return true;
}
  
// Function to perform DFS on the tree
static void dfs(int node, int parent)
{
    int flag = 1;
  
    // Iterating the children of current node
    for (int to : graph[node]) 
    {
  
        // There is at least a child
        // of the current node
        if (to == parent)
            continue;
        flag = 0;
        dfs(to, node);
    }
  
    // Current node is connected to only
    // its parent i.e. it is a leaf node
    if (flag == 1
    {
        // Weight of the current node
        String x = weight[node];
  
        // If the weight is a palindrome
        if (isPalindrome(x))
            cnt += 1;
    }
}
  
// Driver code
public static void main(String[] args)
{
    for(int i = 0; i < graph.length;i++)
        graph[i] = new Vector<Integer>();
          
    // Weights of the node
    weight[1] = "ab";
    weight[2] = "abca";
    weight[3] = "axxa";
    weight[4] = "geeks";
    weight[5] = "aba";
  
    // Edges of the tree
    graph[1].add(2);
    graph[2].add(3);
    graph[2].add(4);
    graph[1].add(5);
  
    dfs(1, 1);
  
    System.out.print(cnt);
}
}
  
// This code is contributed by amal kumar choubey

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG{
  
static int cnt = 0;
  
static List<int> []graph = new List<int>[100];
static String []weight = new String[100];
  
// Function that returns true
// if x is a palindrome
static bool isPalindrome(String x)
{
    int n = x.Length;
    for(int i = 0; i < n / 2; i++) 
    {
       if (x[i] != x[n - 1 - i])
           return false;
    }
    return true;
}
  
// Function to perform DFS on the tree
static void dfs(int node, int parent)
{
    int flag = 1;
  
    // Iterating the children of 
    // current node
    foreach (int to in graph[node]) 
    {
  
        // There is at least a child
        // of the current node
        if (to == parent)
            continue;
        flag = 0;
        dfs(to, node);
    }
  
    // Current node is connected to only
    // its parent i.e. it is a leaf node
    if (flag == 1) 
    {
          
        // Weight of the current node
        String x = weight[node];
  
        // If the weight is a palindrome
        if (isPalindrome(x))
            cnt += 1;
    }
}
  
// Driver code
public static void Main(String[] args)
{
    for(int i = 0; i < graph.Length; i++)
       graph[i] = new List<int>();
          
    // Weights of the node
    weight[1] = "ab";
    weight[2] = "abca";
    weight[3] = "axxa";
    weight[4] = "geeks";
    weight[5] = "aba";
  
    // Edges of the tree
    graph[1].Add(2);
    graph[2].Add(3);
    graph[2].Add(4);
    graph[1].Add(5);
  
    dfs(1, 1);
  
    Console.Write(cnt);
}
}
  
// This code is contributed by amal kumar choubey

chevron_right


Output:

2

competitive-programming-img




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 : Amal Kumar Choubey