Advertisement

Number of Nodes in the Sub-Tree With the Same Label - LeetCode 1519 Solution

Number of Nodes in the Sub-Tree With the Same Label - Complete Solution Guide

Number of Nodes in the Sub-Tree With the Same Label is LeetCode problem 1519, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a tree (i.e. a connected, undirected graph that has no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges . The root of the tree is the node 0 , and each node of the tree has a label which is a lower-case character given in the string labels (i.e. The node with the number i has the label labels[i] ). The edges array is given on the form edges[i] = [a i , b i ] , which means there is an edge between nodes a i and b i in the tree. Return an array of size n

Detailed Explanation

The problem requires us to traverse a tree and, for each node, count the number of nodes in its subtree (including itself) that have the same label as that node. The tree is given by the number of nodes `n`, a list of edges connecting the nodes, and a string `labels` where `labels[i]` represents the label of node `i`. The task is to return an array `ans` where `ans[i]` is the count of nodes in the subtree rooted at node `i` that have the same label as node `i`.

Solution Approach

The provided solution uses a Depth-First Search (DFS) algorithm to traverse the tree. For each node, it recursively visits its children, collecting the label counts from their subtrees. It then adds the node's own label to the count and stores the count of the matching label in the result array. The DFS function returns an array representing the label counts for the entire subtree rooted at that node.

Step-by-Step Algorithm

  1. Step 1: Build an adjacency list representation of the tree from the given `edges`. The adjacency list stores the neighbors of each node.
  2. Step 2: Initialize an array `ans` of size `n` to store the final results. All elements are initially 0.
  3. Step 3: Define a recursive DFS function that takes the current `node` and its `parent` as input.
  4. Step 4: Inside the DFS function, create an array `node_counts` of size 26 to store the counts of each label in the subtree rooted at the current `node`. Initialize all counts to 0.
  5. Step 5: Iterate through the children of the current `node`. If a child is not the parent (to avoid cycles), recursively call the DFS function on the child. Add the label counts returned by the child DFS to `node_counts`.
  6. Step 6: Increment the count of the current `node`'s label in the `node_counts` array.
  7. Step 7: Store the count of the current `node`'s label in the `ans` array at the index corresponding to the current `node`.
  8. Step 8: Return the `node_counts` array.
  9. Step 9: Call the DFS function starting from the root node (node 0) with a parent of -1.
  10. Step 10: Return the `ans` array.

Key Insights

  • Insight 1: The core idea is to use Depth-First Search (DFS) to traverse the tree and compute the counts recursively. Each node's subtree count depends on the counts from its children.
  • Insight 2: A key data structure for storing label counts within the subtree is an array of size 26 (one for each lowercase English letter). This allows efficient aggregation of counts from child subtrees.
  • Insight 3: Efficiently build an adjacency list representation of the tree using the given edges. This allows us to easily iterate through the neighbors of any given node.
  • Insight 4: Handle edge cases such as a single node, disconnected subgraphs, or invalid input, although the constraints guarantee a connected graph.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, Tree, Depth-First Search, Breadth-First Search, Counting.

Companies

Asked at: Samsung.