# Number of special nodes in an n-ary tree

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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// DFS function to traverse the tree and find ` `// number of special nodes ` `void` `dfs(``int` `val[], ``int` `n, vector<``int``> adj[], ``int` `v, ` `         ``unordered_set<``int``>& values, ``int``& ans) ` `{ ` ` `  `    ``// If value of current node is already ` `    ``// present earlier in path then this ` `    ``// node and all other nodes connected to ` `    ``// it are not special ` `    ``if` `(values.count(val[v])) ` `        ``return``; ` ` `  `    ``// Insert value of current node in ` `    ``// set of values traversed ` `    ``ans++; ` `    ``values.insert(val[v]); ` ` `  `    ``// Call dfs on all adjacent nodes ` `    ``for` `(``auto` `ele : adj[v]) { ` `        ``dfs(val, n, adj, ele, values, ans); ` `    ``} ` ` `  `    ``// Erase value of current node as all paths ` `    ``// passing through current node are traversed ` `    ``values.erase(val[v]); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `val[] = { 0, 2, 1, 4, 3, 4, 8, 10, 2, 5, 1 }; ` `    ``int` `n = ``sizeof``(val) / ``sizeof``(val); ` ` `  `    ``vector<``int``> adj[n]; ` ` `  `    ``adj.push_back(2); ` `    ``adj.push_back(3); ` `    ``adj.push_back(4); ` `    ``adj.push_back(5); ` `    ``adj.push_back(6); ` `    ``adj.push_back(7); ` `    ``adj.push_back(8); ` `    ``adj.push_back(9); ` `    ``adj.push_back(10); ` ` `  `    ``unordered_set<``int``> values; ` ` `  `    ``int` `ans = 0; ` ` `  `    ``dfs(val, n, adj, 1, values, ans); ` ` `  `    ``cout << ans; ` ` `  `    ``return` `0; ` `} `

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `static` `int` `ans; ` ` `  `// DFS function to traverse the tree and find ` `// number of special nodes ` `static` `void` `dfs(``int` `val[], ``int` `n, Vector adj[], ``int` `v, ` `        ``HashSet values) ` `{ ` ` `  `    ``// If value of current node is already ` `    ``// present earlier in path then this ` `    ``// node and all other nodes connected to ` `    ``// it are not special ` `    ``if` `(values.contains(val[v])) ` `        ``return``; ` ` `  `    ``// Insert value of current node in ` `    ``// set of values traversed ` `    ``ans++; ` `    ``values.add(val[v]); ` ` `  `    ``// Call dfs on all adjacent nodes ` `    ``for` `(``int` `ele : adj[v]) ` `    ``{ ` `        ``dfs(val, n, adj, ele, values); ` `    ``} ` ` `  `    ``// Erase value of current node as all paths ` `    ``// passing through current node are traversed ` `    ``values.remove(val[v]); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `val[] = { ``0``, ``2``, ``1``, ``4``, ``3``, ``4``, ``8``, ``10``, ``2``, ``5``, ``1` `}; ` `    ``int` `n = val.length; ` ` `  `    ``Vector []adj = ``new` `Vector[n]; ` `    ``for``(``int` `i = ``0``; i < n ; i++)  ` `    ``{ ` `        ``adj[i] = ``new` `Vector(); ` `    ``} ` `    ``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()); ` `    ``System.out.print(ans); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

 `# Python3 implementation of the approach  ` ` `  `# DFS function to traverse the tree  ` `# and find number of special nodes  ` `def` `dfs(val, n, adj, v, values):  ` ` `  `    ``# If value of current node is already  ` `    ``# present earlier in path then this  ` `    ``# node and all other nodes connected ` `    ``# to it are not special  ` `    ``if` `val[v] ``in` `values:  ` `        ``return` `     `  `    ``global` `ans ` ` `  `    ``# Insert value of current node in  ` `    ``# set of values traversed  ` `    ``ans ``+``=` `1` `    ``values.add(val[v])  ` ` `  `    ``# Call dfs on all adjacent nodes  ` `    ``for` `ele ``in` `adj[v]:  ` `        ``dfs(val, n, adj, ele, values)  ` ` `  `    ``# Erase value of current node as all  ` `    ``# paths passing through current node  ` `    ``# are traversed  ` `    ``values.remove(val[v])  ` ` `  `# Driver code  ` `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)  ` ` `  `# This code is contributed by Rituraj Jain `

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` `  `  `static` `int` `ans; ` `  `  `// DFS function to traverse the tree and find ` `// number of special nodes ` `static` `void` `dfs(``int` `[]val, ``int` `n, List<``int``> []adj, ``int` `v, ` `        ``HashSet<``int``> values) ` `{ ` `  `  `    ``// If value of current node is already ` `    ``// present earlier in path then this ` `    ``// node and all other nodes connected to ` `    ``// it are not special ` `    ``if` `(values.Contains(val[v])) ` `        ``return``; ` `  `  `    ``// Insert value of current node in ` `    ``// set of values traversed ` `    ``ans++; ` `    ``values.Add(val[v]); ` `  `  `    ``// Call dfs on all adjacent nodes ` `    ``foreach` `(``int` `ele ``in` `adj[v]) ` `    ``{ ` `        ``dfs(val, n, adj, ele, values); ` `    ``} ` `  `  `    ``// Erase value of current node as all paths ` `    ``// passing through current node are traversed ` `    ``values.Remove(val[v]); ` `} ` `  `  `// Driver code ` `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.Add(2); ` `    ``adj.Add(3); ` `    ``adj.Add(4); ` `    ``adj.Add(5); ` `    ``adj.Add(6); ` `    ``adj.Add(7); ` `    ``adj.Add(8); ` `    ``adj.Add(9); ` `    ``adj.Add(10); ` `  `  `    ``ans = 0; ` `    ``dfs(val, n, adj, 1, ``new` `HashSet<``int``>()); ` `    ``Console.Write(ans); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:
```8
```

Time Complexity: O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

A Programmer and A Machine learning Enthusiast

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 : rituraj_jain, Rajput-Ji

Article Tags :