Dsa
Trees
Medium
Unique Subtree Count
You are given the root of a binary tree. Your task is to count how many unique subtrees are present in this tree. A subtree is considered unique if its structure and node values are unique. Two subtrees are considered the same if they have the same structure and node values, regardless of their position in the tree.
Input Format:
- The first line contains an integer n (the number of nodes).
- The next n lines each contain an integer value representing the node value of the binary tree in level order, with -1 indicating a null node.
Output Format:
- Print a single integer which is the count of unique subtrees.
Example:
Input:
7
1
2
3
4
-1
4
3
Output:
4
Key concepts
treessubtreescounting
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