Dsa
Trees
Medium
The Folder Deduplicator
In a virtual file system, every folder is a node. If two folders have the same set of subfolders (where each subfolder is identical in structure and value), they are considered duplicates. Given a tree where each node has a string label, count how many nodes are the root of a subtree that appears at least once elsewhere in the tree (i.e., there exists another node u such that subtree(u) is identical to subtree(v)).
Input Format:
- Line 1: N, the number of nodes.
- Next N lines: An integer P (parent of node i, -1 for root) and a string S (label of node i).
Output Format:
- The number of nodes that have at least one identical twin subtree elsewhere.
Example:
Input:
3
-1 A
0 B
0 B
Output:
2
(The two 'B' nodes are identical subtrees.)
Key concepts
tree hashingisomorphism
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now