Skip to the code
cf.
+25 XP

#349 Invite List Without Direct Managers

medium

A team offsite planner wants the most enthusiastic guest list. The reporting structure is a binary tree, and each node's value is that person's enthusiasm score. To keep things relaxed, nobody may be invited together with their direct manager (their parent in the tree).

Write bestInviteScore(root) that returns the largest total score of a guest list where no invited person is the direct parent of another invited person. An empty tree returns 0.

Example 1

Input: root = [3,2,3,null,3,null,1]
Output: 7

Invite the root 3, plus the 3 and 1 on the bottom row: 3 + 3 + 1 = 7.

Example 2

Input: root = [3,4,5,1,3,null,1]
Output: 9

Invite 4 and 5, the root's two reports: 4 + 5 = 9.

Constraints

  • 0 <= number of people <= 10^4
  • 0 <= score <= 10^4

Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).