This question evaluates understanding of rooted tree data structures, computation of tree height (reporting layers), and algorithmic manipulation of subtrees to minimize maximum depth, testing skills in tree traversal, depth calculation, and constrained optimization.
You are given an organization chart with n employees labeled 1..n. Employee 1 is the CEO (root).
The reporting relationships are given as two integer arrays manager[] and reportee[] of length n-1, where for each index i:
reportee[i]
reports directly to
manager[i]
Assume these pairs form a valid tree rooted at the CEO.
Define an employee’s reporting layer (level) as their distance from the CEO in nodes, with:
level(1) = 1
(CEO)
2
, etc.
level(v)
over all employees
v
.
h
, deep reporting chains beyond
h
layers are considered harmful. You may perform the following operation any number of times:
x != 1
and change their manager so that
x
now
reports directly to the CEO
(i.e., make
manager(x) = 1
). The entire subtree of
x
moves with them.
Return the minimum number of such moves required so that in the resulting org chart, the maximum reporting layer is ≤ h.
n
, arrays
manager[]
,
reportee[]
, and integer
h
.
h
.
1
.