Count of leaf nodes of the tree whose weighted string is a palindrome
Last Updated :
19 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
vector< int > graph[100];
vector<string> weight(100);
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 ;
}
void dfs( int node, int parent)
{
int flag = 1;
for ( int to : graph[node]) {
if (to == parent)
continue ;
flag = 0;
dfs(to, node);
}
if (flag == 1) {
string x = weight[node];
if (isPalindrome(x))
cnt += 1;
}
}
int main()
{
weight[1] = "ab" ;
weight[2] = "abca" ;
weight[3] = "axxa" ;
weight[4] = "geeks" ;
weight[5] = "aba" ;
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;
}
|
Java
import java.util.*;
class GFG{
static int cnt = 0 ;
static Vector<Integer> []graph = new Vector[ 100 ];
static String []weight = new String[ 100 ];
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 ;
}
static void dfs( int node, int parent)
{
int flag = 1 ;
for ( int to : graph[node])
{
if (to == parent)
continue ;
flag = 0 ;
dfs(to, node);
}
if (flag == 1 )
{
String x = weight[node];
if (isPalindrome(x))
cnt += 1 ;
}
}
public static void main(String[] args)
{
for ( int i = 0 ; i < graph.length;i++)
graph[i] = new Vector<Integer>();
weight[ 1 ] = "ab" ;
weight[ 2 ] = "abca" ;
weight[ 3 ] = "axxa" ;
weight[ 4 ] = "geeks" ;
weight[ 5 ] = "aba" ;
graph[ 1 ].add( 2 );
graph[ 2 ].add( 3 );
graph[ 2 ].add( 4 );
graph[ 1 ].add( 5 );
dfs( 1 , 1 );
System.out.print(cnt);
}
}
|
Python3
cnt = 0
graph = [ 0 ] * 100
for i in range ( 100 ):
graph[i] = []
weight = [ 0 ] * 100
def isPalindrome(x: str ) - > bool :
n = len (x)
for i in range (n / / 2 ):
if (x[i] ! = x[n - 1 - i]):
return False
return True
def dfs(node: int , parent: int ) - > None :
global cnt, graph, weight
flag = 1
for to in graph[node]:
if (to = = parent):
continue
flag = 0
dfs(to, node)
if (flag = = 1 ):
x = weight[node]
if (isPalindrome(x)):
cnt + = 1
if __name__ = = "__main__" :
weight[ 1 ] = "ab"
weight[ 2 ] = "abca"
weight[ 3 ] = "axxa"
weight[ 4 ] = "geeks"
weight[ 5 ] = "aba"
graph[ 1 ].append( 2 )
graph[ 2 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 1 ].append( 5 )
dfs( 1 , 1 )
print (cnt)
|
C#
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];
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 ;
}
static void dfs( int node, int parent)
{
int flag = 1;
foreach ( int to in graph[node])
{
if (to == parent)
continue ;
flag = 0;
dfs(to, node);
}
if (flag == 1)
{
String x = weight[node];
if (isPalindrome(x))
cnt += 1;
}
}
public static void Main(String[] args)
{
for ( int i = 0; i < graph.Length; i++)
graph[i] = new List< int >();
weight[1] = "ab" ;
weight[2] = "abca" ;
weight[3] = "axxa" ;
weight[4] = "geeks" ;
weight[5] = "aba" ;
graph[1].Add(2);
graph[2].Add(3);
graph[2].Add(4);
graph[1].Add(5);
dfs(1, 1);
Console.Write(cnt);
}
}
|
Javascript
<script>
let cnt = 0;
let graph = new Array(100);
let weight = new Array(100);
function isPalindrome(x)
{
let n = x.length;
for (let i = 0; i < parseInt(n / 2, 10); i++)
{
if (x[i] != x[n - 1 - i])
return false ;
}
return true ;
}
function dfs(node, parent)
{
let flag = 1;
for (let to = 0; to < graph[node].length; to++)
{
if (graph[node][to] == parent)
continue ;
flag = 0;
dfs(graph[node][to], node);
}
if (flag == 1)
{
let x = weight[node];
if (isPalindrome(x))
cnt += 1;
}
}
for (let i = 0; i < graph.length;i++)
graph[i] = [];
weight[1] = "ab" ;
weight[2] = "abca" ;
weight[3] = "axxa" ;
weight[4] = "geeks" ;
weight[5] = "aba" ;
graph[1].push(2);
graph[2].push(3);
graph[2].push(4);
graph[1].push(5);
dfs(1, 1);
document.write(cnt);
</script>
|
Time Complexity: O(N) where N is the number of nodes in the tree.
Auxiliary Space: O(1) as constant space is considered.
Share your thoughts in the comments
Please Login to comment...