2003. Smallest Missing Genetic Value in Each Subtree

https://leetcode.com/problems/smallest-missing-genetic-value-in-each-subtree

Description

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

Example 1:

Example 2:

Example 3:

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.

ac

Last updated

Was this helpful?