There is a family tree rooted at 0 consisting of n nodes numbered 0 to n - 1. You are given a 0-indexed integer array parents, where parents[i] is the parent for node i. Since node 0 is the root, parents[0] == -1.
There are 105 genetic values, each represented by an integer in the inclusive range [1, 105]. You are given a 0-indexed integer array nums, where nums[i] is a distinct genetic value for node i.
Return an array ans of length n where ans[i] isthe smallest genetic value that is missing from the subtree rooted at nodei.
The subtree rooted at a node x contains node x and all of its descendant nodes.
Example 1:
Input: parents = [-1,0,0,2], nums = [1,2,3,4]
Output: [5,1,1,1]
Explanation: The answer for each subtree is calculated as follows:
- 0: The subtree contains nodes [0,1,2,3] with values [1,2,3,4]. 5 is the smallest missing value.
- 1: The subtree contains only node 1 with value 2. 1 is the smallest missing value.
- 2: The subtree contains nodes [2,3] with values [3,4]. 1 is the smallest missing value.
- 3: The subtree contains only node 3 with value 4. 1 is the smallest missing value.
Example 2:
Input: parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3]
Output: [7,1,1,4,2,1]
Explanation: The answer for each subtree is calculated as follows:
- 0: The subtree contains nodes [0,1,2,3,4,5] with values [5,4,6,2,1,3]. 7 is the smallest missing value.
- 1: The subtree contains nodes [1,2] with values [4,6]. 1 is the smallest missing value.
- 2: The subtree contains only node 2 with value 6. 1 is the smallest missing value.
- 3: The subtree contains nodes [3,4,5] with values [2,1,3]. 4 is the smallest missing value.
- 4: The subtree contains only node 4 with value 1. 2 is the smallest missing value.
- 5: The subtree contains only node 5 with value 3. 1 is the smallest missing value.
Example 3:
Input: parents = [-1,2,3,0,2,4,1], nums = [2,3,4,5,6,7,8]
Output: [1,1,1,1,1,1,1]
Explanation: The value 1 is missing from all the subtrees.
Constraints:
n == parents.length == nums.length
2 <= n <= 105
0 <= parents[i] <= n - 1 for i != 0
parents[0] == -1
parents represents a valid tree.
1 <= nums[i] <= 105
Each nums[i] is distinct.
Solutions
Solution 1: DFS
Thinking
With \(n \le 10^5\), scanning every subtree separately revisits nodes too often. Gene values are unique, so mex \(> 1\) can occur only on the path from the node with gene \(1\) to the root; every other node answers \(1\).
Walking that path upward only enlarges the gene set (the parent adds sibling subtrees). Already marked genes need not be rescanned.
DFS from \(\textit{idx}\) toward the root fills \(\textit{has}\); a monotone pointer \(i\) finds the smallest missing value and writes \(\textit{ans}[\textit{idx}]\).
We notice that each node has a unique gene value, so we only need to find the node \(\textit{idx}\) with gene value \(1\), and all nodes except for those on the path from node \(\textit{idx}\) to the root node \(0\) have an answer of \(1\).
Therefore, we initialize the answer array \(\textit{ans}\) to \([1,1,...,1]\), and our focus is on finding the answer for each node on the path from node \(\textit{idx}\) to the root node \(0\).
We can start from node \(\textit{idx}\) and use depth-first search to mark the gene values that appear in the subtree rooted at \(\textit{idx}\), and record them in the array \(\textit{has}\). During the search process, we use an array \(\textit{vis}\) to mark the visited nodes to prevent repeated visits.
Next, we start from \(i=2\) and keep looking for the first gene value that has not appeared, which is the answer for node \(\textit{idx}\). Here, \(i\) is strictly increasing, because the gene values are unique, so we can always find a gene value that has not appeared in \([1,..n+1]\).
Then, we update the answer for node \(\textit{idx}\), i.e., \(\textit{ans}[\textit{idx}]=i\), and update \(\textit{idx}\) to its parent node to continue the above process until \(\textit{idx}=-1\), which means we have reached the root node \(0\).
Finally, we return the answer array \(\textit{ans}\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of nodes.
With \(n \le 10^5\), scanning every subtree separately revisits nodes too often. Gene values are unique, so mex \(> 1\) can occur only on the path from the node with gene \(1\) to the root; every other node answers \(1\). If gene \(1\) sits at the root of a chain, recursively marking that subtree has call depth \(n\).
Walking that path upward only enlarges the gene set (the parent adds sibling subtrees). Already marked genes need not be rescanned.
An explicit stack marks the subtree from \(\textit{idx}\) toward the root and fills \(\textit{has}\). A monotone pointer \(i\) finds the smallest missing value and writes \(\textit{ans}[\textit{idx}]\).
We notice that each node has a unique gene value, so we only need to find the node \(\textit{idx}\) with gene value \(1\), and all nodes except for those on the path from node \(\textit{idx}\) to the root node \(0\) have an answer of \(1\).
Therefore, we initialize the answer array \(\textit{ans}\) to \([1,1,...,1]\), and our focus is on finding the answer for each node on the path from node \(\textit{idx}\) to the root node \(0\).
We can start from node \(\textit{idx}\) and use an explicit stack to mark the gene values that appear in the subtree rooted at \(\textit{idx}\), and record them in the array \(\textit{has}\). During the walk, we use an array \(\textit{vis}\) to mark the visited nodes to prevent repeated visits.
Next, we start from \(i=2\) and keep looking for the first gene value that has not appeared, which is the answer for node \(\textit{idx}\). Here, \(i\) is strictly increasing, because the gene values are unique, so we can always find a gene value that has not appeared in \([1,..n+1]\).
Then, we update the answer for node \(\textit{idx}\), i.e., \(\textit{ans}[\textit{idx}]=i\), and update \(\textit{idx}\) to its parent node to continue the above process until \(\textit{idx}=-1\), which means we have reached the root node \(0\).
Finally, we return the answer array \(\textit{ans}\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of nodes.