Number of Nodes in the Sub-Tree With the Same Label - Problem
Tree Node Counting Challenge

Imagine you're analyzing a company's organizational chart where each employee has a department label (like 'a', 'b', 'c'). Your task is to help each employee count how many people in their entire team hierarchy (including themselves) work in the same department.

You're given:
• A tree structure with n employees numbered from 0 to n-1
• Employee 0 is the CEO (root of the tree)
• Each employee has a department label from the string labels
• The edges array shows the reporting relationships

Goal: For each employee i, count how many people in their subtree (team + all subordinates) have the same department label as employee i.

Example: If employee 3 works in department 'b' and has 2 subordinates also in department 'b', the answer for employee 3 would be 3 (including themselves).

Input & Output

example_1.py — Basic Tree
$ Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd"
Output: [2,1,1,1,1,1,1]
💡 Note: Node 0 has label 'a' and its subtree contains nodes {0,1,2,3,4,5,6} with labels 'abaedcd'. Only nodes 0 and 2 have label 'a', so result[0] = 2. Each other node has a unique label in their subtree, so they all get count 1.
example_2.py — Multiple Same Labels
$ Input: n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb"
Output: [4,2,1,1]
💡 Note: All nodes have label 'b'. Node 0's subtree includes all nodes {0,1,2,3}, so result[0] = 4. Node 1's subtree includes {1,2}, so result[1] = 2. Nodes 2 and 3 are leaves with only themselves, so result[2] = result[3] = 1.
example_3.py — Single Node
$ Input: n = 1, edges = [], labels = "a"
Output: [1]
💡 Note: Only one node with label 'a'. Its subtree contains only itself, so the result is [1].

Constraints

  • 1 ≤ n ≤ 105
  • edges.length == n - 1
  • edges[i].length == 2
  • 0 ≤ ai, bi < n
  • ai ≠ bi
  • labels.length == n
  • labels consists of lowercase English letters

Visualization

Tap to expand
CEO(a)VP(b)CTO(a)Mgr(b)Dir(c)Dev(a)Department Count ResultsCEO: 3 people in dept 'a' | VP: 2 people in dept 'b' | CTO: 2 people in dept 'a'
Understanding the Visualization
1
Build the Organization Chart
Convert the edges into a tree structure representing reporting relationships
2
Start from the Bottom
Use post-order DFS to process leaf employees first, then move up the hierarchy
3
Aggregate Department Counts
Each manager collects department statistics from their direct reports
4
Add Own Department
Each person adds their own department to the count and records their result
Key Takeaway
🎯 Key Insight: Use post-order DFS with frequency arrays to count labels efficiently in a single traversal, avoiding redundant subtree visits.
Asked in
Facebook 35 Amazon 28 Google 22 Microsoft 18
68.0K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen