1519. Number of Nodes in the Sub-Tree With the Same Label Medium
1/**
2 * [1519] Number of Nodes in the Sub-Tree With the Same Label
3 *
4 * 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]).
5 * The edges array is given on the form edges[i] = [ai, bi], which means there is an edge between nodes ai and bi in the tree.
6 * Return an array of size n where ans[i] is the number of nodes in the subtree of the i^th node which have the same label as node i.
7 * A subtree of a tree T is the tree consisting of a node in T and all of its descendant nodes.
8 *
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e1.jpg" style="width: 400px; height: 291px;" />
11 * Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd"
12 * Output: [2,1,1,1,1,1,1]
13 * Explanation: Node 0 has label 'a' and its sub-tree has node 2 with label 'a' as well, thus the answer is 2. Notice that any node is part of its sub-tree.
14 * Node 1 has a label 'b'. The sub-tree of node 1 contains nodes 1,4 and 5, as nodes 4 and 5 have different labels than node 1, the answer is just 1 (the node itself).
15 *
16 * Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e2.jpg" style="width: 300px; height: 253px;" />
18 * Input: n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb"
19 * Output: [4,2,1,1]
20 * Explanation: The sub-tree of node 2 contains only node 2, so the answer is 1.
21 * The sub-tree of node 3 contains only node 3, so the answer is 1.
22 * The sub-tree of node 1 contains nodes 1 and 2, both have label 'b', thus the answer is 2.
23 * The sub-tree of node 0 contains nodes 0, 1, 2 and 3, all with label 'b', thus the answer is 4.
24 *
25 * Example 3:
26 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e3.jpg" style="width: 300px; height: 253px;" />
27 * Input: n = 5, edges = [[0,1],[0,2],[1,3],[0,4]], labels = "aabab"
28 * Output: [3,2,1,1,1]
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= n <= 10^5
34 * edges.length == n - 1
35 * edges[i].length == 2
36 * 0 <= ai, bi < n
37 * ai != bi
38 * labels.length == n
39 * labels is consisting of only of lowercase English letters.
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/
45// discuss: https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn count_sub_trees(n: i32, edges: Vec<Vec<i32>>, labels: String) -> Vec<i32> {
51 vec![]
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_1519() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.