Back

Best Downward Path Sum in a Tree

OAAsync OALast reported April 2025Low Frequency

Problem Overview

Given a tree with n nodes, rooted at node 0 (nodes are numbered 0 to n-1), where values[i] is the value of node i, find the maximal sum of values along any path that starts at some node u and goes only down the tree. In other words, only consider paths u1, u2, ..., uk where each node ui is a child of u(i-1) for 1 < i <= k. Values can be positive or negative.

Complete the function bestSumDownwardTreePath. It must return an integer: the largest sum of values along a path down the tree from any node u.

Parameters

  • parent[0..n-1]: parent[i] is the parent of node i; parent[i] = -1 means node i is the root.
  • values[0..n-1]: values[i] is the value of node i.

Example

node / value:

        0/5
       /    \
     1/7    4/15
      |
    2/-10
      |
     3/4

parent = [-1, 0, 1, 2, 0]
values = [5, 7, -10, 4, 15]

0 -> 1 -> 2 -> 3   5 + 7 + (-10) + 4 = 6
1 -> 2 -> 3        7 + (-10) + 4     = 1
0 -> 4             5 + 15            = 20   <- the highest

bestSumDownwardTreePath(parent, values) -> 20

Notes

  • A path can start at any node, not only the root, and can stop at any node: the definition above does not require it to end at a leaf. A single node is a path (k = 1).
  • The posted prompt does not say that a parent always has a smaller index than its children (the example happens to), and it gives no limits on n or on the values. Confirm both with the platform's constraints if they are shown.

What Reports Emphasize

What passers do: Solved with a DFS (the April 2025 candidate: the first question was solved with DFS).

Practice

Write your own against 7 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Citadel with friends?

Send them this page. It is free to read, no account needed.

More Citadel Questions

Free preview

Every question in the Citadel catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 62 Citadel questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Citadel · OA · Reported 2× across candidate reports
Is this helpful?