Number of special nodes in an n-ary tree
Last Updated :
06 Aug, 2021
Given an n-ary tree rooted at vertex 1. The tree has n vertices and n-1 edges. Each node has a value associated with it and tree is input in the form of adjacency list. The task is to find the number of special nodes in the tree. A node is special if the path from the root to the node consists of distinct value nodes.
Examples:
Input: val[] = {1, 2, 3, 4, 5, 7, 2, 3}
1
/ \
2 3
/ \ \
4 5 7
/ \
2 3
Output: 7
All nodes except the leaf node 2 are special.
Input: val[] = {2, 1, 4, 3, 4, 8, 10, 2, 5, 1}
2
/ \
1 4
/ \ \ \
3 4 8 10
/ \ \
2 5 1
Output: 8
Leaf nodes 2 and 1 are not special.
Approach: The idea is to perform dfs on given tree using adjacency list. While performing dfs insert values of nodes visited in a set. If value of current node is already present in the set then current node and all nodes in the subtree rooted at current node are not special. After traversing subtree rooted at current node erase the value of current node from set as this value or node does not lie on path from root to all other unvisited nodes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void dfs( int val[], int n, vector< int > adj[], int v,
unordered_set< int >& values, int & ans)
{
if (values.count(val[v]))
return ;
ans++;
values.insert(val[v]);
for ( auto ele : adj[v]) {
dfs(val, n, adj, ele, values, ans);
}
values.erase(val[v]);
}
int main()
{
int val[] = { 0, 2, 1, 4, 3, 4, 8, 10, 2, 5, 1 };
int n = sizeof (val) / sizeof (val[0]);
vector< int > adj[n];
adj[1].push_back(2);
adj[1].push_back(3);
adj[2].push_back(4);
adj[2].push_back(5);
adj[2].push_back(6);
adj[3].push_back(7);
adj[5].push_back(8);
adj[5].push_back(9);
adj[5].push_back(10);
unordered_set< int > values;
int ans = 0;
dfs(val, n, adj, 1, values, ans);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int ans;
static void dfs( int val[], int n, Vector<Integer> adj[], int v,
HashSet<Integer> values)
{
if (values.contains(val[v]))
return ;
ans++;
values.add(val[v]);
for ( int ele : adj[v])
{
dfs(val, n, adj, ele, values);
}
values.remove(val[v]);
}
public static void main(String[] args)
{
int val[] = { 0 , 2 , 1 , 4 , 3 , 4 , 8 , 10 , 2 , 5 , 1 };
int n = val.length;
Vector<Integer> []adj = new Vector[n];
for ( int i = 0 ; i < n ; i++)
{
adj[i] = new Vector<Integer>();
}
adj[ 1 ].add( 2 );
adj[ 1 ].add( 3 );
adj[ 2 ].add( 4 );
adj[ 2 ].add( 5 );
adj[ 2 ].add( 6 );
adj[ 3 ].add( 7 );
adj[ 5 ].add( 8 );
adj[ 5 ].add( 9 );
adj[ 5 ].add( 10 );
ans = 0 ;
dfs(val, n, adj, 1 , new HashSet<Integer>());
System.out.print(ans);
}
}
|
Python3
def dfs(val, n, adj, v, values):
if val[v] in values:
return
global ans
ans + = 1
values.add(val[v])
for ele in adj[v]:
dfs(val, n, adj, ele, values)
values.remove(val[v])
if __name__ = = "__main__" :
val = [ 0 , 2 , 1 , 4 , 3 , 4 , 8 , 10 , 2 , 5 , 1 ]
n = len (val)
adj = [[] for i in range (n)]
adj[ 1 ].append( 2 )
adj[ 1 ].append( 3 )
adj[ 2 ].append( 4 )
adj[ 2 ].append( 5 )
adj[ 2 ].append( 6 )
adj[ 3 ].append( 7 )
adj[ 5 ].append( 8 )
adj[ 5 ].append( 9 )
adj[ 5 ].append( 10 )
values = set ()
ans = 0
dfs(val, n, adj, 1 , values)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int ans;
static void dfs( int []val, int n, List< int > []adj, int v,
HashSet< int > values)
{
if (values.Contains(val[v]))
return ;
ans++;
values.Add(val[v]);
foreach ( int ele in adj[v])
{
dfs(val, n, adj, ele, values);
}
values.Remove(val[v]);
}
public static void Main(String[] args)
{
int []val = { 0, 2, 1, 4, 3, 4, 8, 10, 2, 5, 1 };
int n = val.Length;
List< int > []adj = new List< int >[n];
for ( int i = 0; i < n ; i++)
{
adj[i] = new List< int >();
}
adj[1].Add(2);
adj[1].Add(3);
adj[2].Add(4);
adj[2].Add(5);
adj[2].Add(6);
adj[3].Add(7);
adj[5].Add(8);
adj[5].Add(9);
adj[5].Add(10);
ans = 0;
dfs(val, n, adj, 1, new HashSet< int >());
Console.Write(ans);
}
}
|
Javascript
<script>
var ans;
function dfs(val , n, adj , v, values) {
if (values.has(val[v]))
return ;
ans++;
values.add(val[v]);
adj[v].forEach( function (ele) {
dfs(val, n, adj, ele, values);
});
values. delete (val[v]);
}
var val = [ 0, 2, 1, 4, 3, 4, 8, 10, 2, 5, 1 ];
var n = val.length;
var adj = [];
for ( var i = 0; i < n; i++) {
adj[i] = [];
}
adj[1].push(2);
adj[1].push(3);
adj[2].push(4);
adj[2].push(5);
adj[2].push(6);
adj[3].push(7);
adj[5].push(8);
adj[5].push(9);
adj[5].push(10);
ans = 0;
dfs(val, n, adj, 1, new Set());
document.write(ans);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...