Dsa
Trees
Medium
The Integrity Trail
In a corporate network, each employee node has an 'Integrity Score'. A path starting from the CEO (root, node 0) is considered an 'Integrity Trail' if the integrity scores of the employees on the path are strictly increasing as you move away from the CEO. Given the tree structure and scores, find the length of the longest Integrity Trail starting from the root.
Input Format:
- The first line contains N, the number of employees.
- The second line contains N space-separated integers, where the i-th integer is the integrity score of employee i.
- The next N-1 lines each contain two integers, u and v, representing a management connection (edge).
Output Format:
- A single integer representing the maximum number of nodes in a valid Integrity Trail starting from node 0.
Example:
Input:
5
10 20 15 25 30
0 1
0 2
1 3
1 4
Output:
3
Explanation: Trails are 0->1->3 (10, 20, 25) and 0->1->4 (10, 20, 30). Both have length 3. 0->2 is (10, 15) length 2.
Key concepts
treedepth-first searchrecursion
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