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: [2, 6, 1, 1, 1]
๐Ÿ’ก Note: Root (genetic value 1) has subtree {1,3,2,5,4}, missing 2. Node 1 (value 3) has subtree {3,5,4}, missing 6. 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: [1, 1, 2, 1]
๐Ÿ’ก Note: Root has value 2, doesn't contain 1 in its subtree, so answer is 1. Node 1 has value 3, also missing 1. 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
1Missing: 23Missing: 62Missing: 15Missing: 14Missing: 1DFS Traversal PathRoot โ†’ Node 3 โ†’ Node 5 (leaf)Genetic Set: {1} โ†’ {1,3} โ†’ {1,3,5}Missing Values: 2, 2, 2Key InsightEach subtree inherits genetic markersfrom ancestors during DFS traversalBacktrack removes current marker
Understanding the Visualization
1
Build Family Tree
Create tree structure from parent-child relationships
2
Start DFS from Root
Begin depth-first traversal from the family patriarch/matriarch
3
Track Genetic Values
Maintain set of genetic markers found in current lineage
4
Find Missing Marker
For each person, identify smallest missing genetic value in their lineage
5
Backtrack Efficiently
Remove current genetic value when moving back up the family tree
Key Takeaway
๐ŸŽฏ Key Insight: Use DFS to efficiently track genetic values in each lineage, finding missing markers in O(n) time by leveraging the tree structure and smart backtracking.
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