Dsa
Trees
Medium
The Corporate Median Vibe
In a company tree, every employee has a 'base vibe' score. A manager's 'final vibe' is calculated as the median of their own base vibe and the final vibes of all their direct reports. If there's an even number of values, the median is the average of the middle two. Calculate the final vibe of the CEO (root node 0). Use floating point for all vibes.
Input Format:
Line 1: N (number of employees)
Line 2: N floats (base vibes of employees 0 to N-1)
Next N-1 lines: u v (manager u, report v)
Example:
Input:
3
10.0 20.0 30.0
0 1
0 2
Output:
20.0000
Key concepts
post-order traversalmediann-ary tree
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now