Skip to content

Practice Smallest Missing Genetic Value in Each Subtree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2003
LeetCode Problem

Smallest Missing Genetic Value in Each Subtree

Problem Statement

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 10^5 genetic values, each represented by an integer in the inclusive range [1, 10^5]. 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] is the smallest genetic value that is missing from the subtree rooted at node i. The subtree rooted at a node x contains node x and all of its descendant nodes.

Source: Smallest Missing Genetic Value in Each Subtree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
parents = [-1,0,0,2], nums = [1,2,3,4]
Output
[5,1,1,1]

# Example 2

Input
parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3]
Output
[7,1,1,4,2,1]

# 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]

Constraints

n == parents.length == nums.length
2 <= n <= 10^5
0 <= parents[i] <= n - 1 for i != 0
parents[0] == -1
parents represents a valid tree.
1 <= nums[i] <= 10^5
Each nums[i] is distinct.

Related LeetCode Problems

Practice more problems similar to Smallest Missing Genetic Value in Each Subtree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.