|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../water-bottles "Water Bottles") |
| 9 | + |
| 10 | +[Next >](../maximum-number-of-non-overlapping-substrings "Maximum Number of Non-Overlapping Substrings") |
| 11 | + |
| 12 | +## [1519. Number of Nodes in the Sub-Tree With the Same Label (Medium)](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label "子树中标签相同的节点数") |
| 13 | + |
| 14 | +<p>Given a tree (i.e. a connected, undirected graph that has no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> <code>edges</code>. The <strong>root</strong> of the tree is the node <code>0</code>, and each node of the tree has <strong>a label</strong> which is a lower-case character given in the string <code>labels</code> (i.e. The node with the number <code>i</code> has the label <code>labels[i]</code>).</p> |
| 15 | + |
| 16 | +<p>The <code>edges</code> array is given on the form <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>, which means there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p> |
| 17 | + |
| 18 | +<p>Return <em>an array of size <code>n</code></em> where <code>ans[i]</code> is the number of nodes in the subtree of the <code>i<sup>th</sup></code> node which have the same label as node <code>i</code>.</p> |
| 19 | + |
| 20 | +<p>A subtree of a tree <code>T</code> is the tree consisting of a node in <code>T</code> and all of its descendant nodes.</p> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong>Example 1:</strong></p> |
| 24 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e1.jpg" style="width: 441px; height: 321px;" /> |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd" |
| 27 | +<strong>Output:</strong> [2,1,1,1,1,1,1] |
| 28 | +<strong>Explanation:</strong> 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. |
| 29 | +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). |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong>Example 2:</strong></p> |
| 33 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e2.jpg" style="width: 381px; height: 321px;" /> |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb" |
| 36 | +<strong>Output:</strong> [4,2,1,1] |
| 37 | +<strong>Explanation:</strong> The sub-tree of node 2 contains only node 2, so the answer is 1. |
| 38 | +The sub-tree of node 3 contains only node 3, so the answer is 1. |
| 39 | +The sub-tree of node 1 contains nodes 1 and 2, both have label 'b', thus the answer is 2. |
| 40 | +The sub-tree of node 0 contains nodes 0, 1, 2 and 3, all with label 'b', thus the answer is 4. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p><strong>Example 3:</strong></p> |
| 44 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e3.jpg" style="width: 381px; height: 321px;" /> |
| 45 | +<pre> |
| 46 | +<strong>Input:</strong> n = 5, edges = [[0,1],[0,2],[1,3],[0,4]], labels = "aabab" |
| 47 | +<strong>Output:</strong> [3,2,1,1,1] |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p><strong>Example 4:</strong></p> |
| 51 | + |
| 52 | +<pre> |
| 53 | +<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,3],[3,4],[4,5]], labels = "cbabaa" |
| 54 | +<strong>Output:</strong> [1,2,1,1,2,1] |
| 55 | +</pre> |
| 56 | + |
| 57 | +<p><strong>Example 5:</strong></p> |
| 58 | + |
| 59 | +<pre> |
| 60 | +<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6]], labels = "aaabaaa" |
| 61 | +<strong>Output:</strong> [6,5,4,1,3,2,1] |
| 62 | +</pre> |
| 63 | + |
| 64 | +<p> </p> |
| 65 | +<p><strong>Constraints:</strong></p> |
| 66 | + |
| 67 | +<ul> |
| 68 | + <li><code>1 <= n <= 10^5</code></li> |
| 69 | + <li><code>edges.length == n - 1</code></li> |
| 70 | + <li><code>edges[i].length == 2</code></li> |
| 71 | + <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li> |
| 72 | + <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> |
| 73 | + <li><code>labels.length == n</code></li> |
| 74 | + <li><code>labels</code> is consisting of only of lower-case English letters.</li> |
| 75 | +</ul> |
| 76 | + |
| 77 | +### Related Topics |
| 78 | + [[Depth-first Search](../../tag/depth-first-search/README.md)] |
| 79 | + [[Breadth-first Search](../../tag/breadth-first-search/README.md)] |
| 80 | + |
| 81 | +### Hints |
| 82 | +<details> |
| 83 | +<summary>Hint 1</summary> |
| 84 | +Start traversing the tree and each node should return a vector to its parent node. |
| 85 | +</details> |
| 86 | + |
| 87 | +<details> |
| 88 | +<summary>Hint 2</summary> |
| 89 | +The vector should be of length 26 and have the count of all the labels in the sub-tree of this node. |
| 90 | +</details> |
0 commit comments