KatInterview

Subtree Totals Under Collapse and Graft Operations

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designtreehash-mapsimulation

Last updated

Maintain per-node totals in a rooted tree while nodes are collapsed into leaves or have a subtree grafted onto a leaf, answering total queries after each change.

You are given a rooted tree in which every node has a unique integer id. A node is described by a dictionary with the keys "id", "value" and "children" (a list of node dictionaries, possibly empty or missing).

  • A node with no children is a leaf, and its own "value" is its total.
  • A node with at least one child is internal. Its "value" field is ignored, and its total is the sum of the totals of its direct children.

You must process a sequence of commands against this tree and report results. Commands are given as two parallel lists, operations and arguments; arguments[i] holds the arguments for operations[i].

OperationArgumentsEffectResult
"getValue"[node_id]Report the current total of that node.the integer total
"toLeaf"[node_id, value]The node becomes a leaf with total value. All of its descendants are removed from the tree. If it was already a leaf, only its value changes.None
"toParent"[node_id, subtree]The node, which is currently a leaf, becomes an internal node whose only child is the root of subtree (a node dictionary in the same format as above, with ids not present in the tree). The node's total becomes the total of that subtree root.None

Totals of all ancestors must reflect every change, so that any later getValue returns the correct total for the current shape of the tree. Applying the commands to the root itself is allowed.

Return a list with one entry per operation, in order: an integer for getValue, and None for toLeaf and toParent.

Implement sum_tree_session(initial_tree, operations, arguments).

Constraints

  • All ids, including those in grafted subtrees, are unique across the tree at any moment.
  • Values are integers and may be negative or zero.
  • getValue is only called on ids that currently exist.
  • toParent is only called on a node that is currently a leaf.
  • A node dictionary may omit "children" or give an empty list; both mean leaf.
  • Reads should be fast: aim for far less work per query than recomputing a subtree sum.

Examples

Example 1

Input: sum_tree_session({'id': 1, 'value': 0, 'children': [{'id': 2, 'value': 3, 'children': []}, {'id': 3, 'value': 0, 'children': [{'id': 4, 'value': 5, 'children': []}, {'id': 5, 'value': -1, 'children': []}]}]}, ['getValue', 'getValue', 'toLeaf', 'getValue', 'toParent', 'getValue', 'getValue'], [[1], [3], [3, 7], [1], [3, {'id': 6, 'value': 0, 'children': [{'id': 7, 'value': 2, 'children': []}, {'id': 8, 'value': 4, 'children': []}]}], [1], [3]])

Output: [7, 4, None, 10, None, 9, 6]

Example 2

Input: sum_tree_session({'id': 10, 'value': 4, 'children': []}, ['getValue', 'toParent', 'getValue', 'toLeaf', 'getValue'], [[10], [10, {'id': 11, 'value': 0, 'children': [{'id': 12, 'value': -2, 'children': []}]}], [10], [10, 0], [10]])

Output: [4, None, -2, None, 0]

Hints

Hint 1

If you store the total of every node, a query becomes a single lookup. The work moves into the commands that change the tree.

Hint 2

When one node's total changes by some delta, each of its ancestors changes by exactly that delta and nothing else changes. Keep parent pointers so you can walk upward.

Hint 3

When collapsing a node into a leaf, delete all descendants from your maps so no stale entries remain. When grafting, load the new subtree bottom-up so its totals are computed before you attach it.