Smallest Missing Genetic Value in Each Subtree - Problem

You are a geneticist studying a family tree represented as a tree data structure. The tree consists of n individuals numbered 0 to n - 1, where person 0 is the common ancestor (root).

Each person in the family carries a unique genetic marker - an integer value between 1 and 105. Your task is to analyze each person's "genetic lineage" (their subtree) and find the smallest positive genetic value that doesn't exist in their family line.

Input:

  • parents: Array where parents[i] is the parent of person i (with parents[0] = -1 for root)
  • nums: Array where nums[i] is the genetic value of person i

Goal: Return an array where each element represents the smallest missing genetic value in that person's subtree (including themselves and all descendants).

A subtree rooted at node x contains node x and all nodes that can be reached by following parent-child relationships downward from x.

Input & Output

example_1.py — Basic Family Tree
$ Input: parents = [-1, 0, 0, 1, 1], nums = [1, 3, 2, 5, 4]
Output: [6, 1, 1, 1, 1]
💡 Note: Root (genetic value 1) has subtree {1,2,3,4,5}, missing 6. Node 1 (value 3) has subtree {3,5,4}, missing 1. Leaves have subtree containing only themselves, so missing value is 1 unless they have genetic value 1.
example_2.py — Linear Family Tree
$ Input: parents = [-1, 0, 1, 2], nums = [2, 3, 1, 4]
Output: [5, 2, 2, 1]
💡 Note: Root has subtree {2,3,1,4} = {1,2,3,4}, missing 5. Node 1 has subtree {3,1,4}, missing 2. Node 2 has subtree {1,4}, missing 2. Leaf node 3 has only value 4, missing 1.
example_3.py — Single Node
$ Input: parents = [-1], nums = [1]
Output: [2]
💡 Note: Single node with genetic value 1. The subtree contains {1}, so the smallest missing positive value is 2.

Constraints

  • n == parents.length == nums.length
  • 1 ≤ n ≤ 105
  • parents[0] == -1
  • For i ≠ 0, 0 ≤ parents[i] ≤ n - 1
  • parents represents a valid tree
  • 1 ≤ nums[i] ≤ 105
  • All values in nums are distinct

Visualization

Tap to expand
Smallest Missing Genetic Value in Each Subtree INPUT Family Tree Structure 0 val=1 1 val=3 2 val=2 3 val=5 4 val=4 parents array: -1 0 0 1 1 nums array: 1 3 2 5 4 i=0 i=1 i=2 i=3 i=4 ALGORITHM - DFS 1 Find node with value 1 Locate the starting point Node 0 has val=1 2 DFS from node with 1 Traverse path to root Path: 0 (root) 3 Collect subtree values Track seen genetic vals 4 Find smallest missing Increment counter until value not in set Subtree Analysis: Node 3: subtree={5} miss=1 Node 4: subtree={4} miss=1 Node 2: subtree={2} miss=1 Node 1: subtree={3,4,5} miss=1 Node 0: {1,2,3,4,5} miss=6 *Node 1 inherits from 0: miss=6 FINAL RESULT Each node shows smallest missing value 0 ans=6 1 ans=6 2 ans=1 3 ans=1 4 ans=1 Output Array: 6 6 1 1 1 [2, 6, 1, 1, 1] OK - Computed successfully! Key Insight: Only nodes on the path from the node containing value 1 to the root can have a missing value greater than 1. All other nodes don't have 1 in their subtree, so their answer is always 1. This optimization reduces complexity from O(n^2) to O(n) by only processing the critical path and propagating values upward. TutorialsPoint - Smallest Missing Genetic Value in Each Subtree | DFS Approach
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen