Company hierarchy and salary
Filed under Google
Free read 1 of 3Confirm that an N-ary tree node structure is used with a list of child references.
The question
Given the root of an N-ary tree representing a company hierarchy where each node contains an employee's salary and a list of subordinate nodes, write a function to calculate the total salary sum for a given employee and all their subordinates recursively.
Constraints
- The tree can have an arbitrary number of children per node (N-ary).
- Salary values are non-negative integers.
- Node count can be up to 10^5.
Examples
- Input
- root = [100, [ [50, []], [30, []] ]]
- Output
- 180
- Why
- The root employee has a salary of 100 and two subordinates with salaries 50 and 30. Total sum = 100 + 50 + 30 = 180.
Approach
Confirm that an N-ary tree node structure is used with a list of child references.
Define a recursive function that adds the current node's value and sums the result of recursive calls on all its children.
Handle empty trees or nodes with empty child lists correctly.
Solution
O(N) time, O(H) space where H is tree height
class Node:
def __init__(self, val, children=None):
self.val = val
self.children = children or []
def total_salary(root: Node) -> int:
if not root:
return 0
current = root.val
for child in root.children:
current += total_salary(child)
return current
Tips
- What they are testing: Test with a flat hierarchy, a deep linear hierarchy, and a node with no subordinates.
- Common mistake: Assuming a binary tree structure instead of properly iterating through a dynamic list of children.
Follow-ups
- A slightly harder version of the initial problem.
Related Google questions
Collected from public candidate posts and coding forums. Updated Nov 2026.