KatInterview

Count Node Changes Between Two Keyed Hierarchies

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphstreedfshash-map

Last updated

Compare two keyed trees in parallel and count changed nodes, where a changed value replaces its whole subtree and unmatched nodes count with their subtrees.

Counting Changes Between Two Hierarchies

A hierarchy is stored as nested dictionaries. Each node has this form:

{"key": <string>, "value": <any JSON value>, "children": [<node>, ...]}

An empty hierarchy is None. Among siblings, every key is distinct. The order of a node's children carries no meaning.

You are given two hierarchies, left and right. Return the total number of nodes that differ between them, using the rules below.

Rules

A node is identified by its path: the sequence of keys leading from the root down to that node. Two nodes in different trees are the "same position" only when their paths are identical.

  1. Only on one side. If a node's path exists in just one tree, that node counts as one change, and every node beneath it in that tree counts too (the whole subtree).
  2. Same path, different value. If both trees have a node at the same path but their value fields are not equal, treat the whole subtree as removed and rebuilt. Add the number of nodes in the left subtree plus the number of nodes in the right subtree. Do not look at descendants separately: nodes below it count even if they look identical on both sides.
  3. Same path, equal value. The node itself adds nothing. Compare its children using these same rules, matching children by key regardless of order.
  4. Roots. The two roots are compared as being at the same position. If one tree is None, the answer is the node count of the other tree (0 if both are None). If both roots exist but have different keys, treat that as the "different value" case: the answer is the sum of both tree sizes.

A missing "value" or "children" entry may be treated as None and as an empty list respectively. Values are compared with Python ==/!=.

Task

Implement the function so it returns the total number of differing nodes as an integer.

Constraints

  • Each tree has up to roughly 10^5 nodes; trees can be very deep (a long chain), so avoid relying on Python's recursion limit.
  • Sibling keys are unique within each tree.
  • Keys are strings; values are arbitrary JSON values.
  • The input must not be modified.

Examples

Example 1

Input: count_tree_changes(None, {'key': 'root', 'value': 1, 'children': []})

Output: 1

Example 2

Input: count_tree_changes({'key': 'root', 'value': 'v', 'children': [{'key': 'a', 'value': 1, 'children': []}, {'key': 'b', 'value': 2, 'children': []}]}, {'key': 'root', 'value': 'v', 'children': [{'key': 'b', 'value': 2, 'children': []}, {'key': 'c', 'value': 3, 'children': []}]})

Output: 2

Example 3

Input: count_tree_changes({'key': 'root', 'value': 1, 'children': [{'key': 'a', 'value': 10, 'children': [{'key': 'x', 'value': 1, 'children': []}]}]}, {'key': 'root', 'value': 2, 'children': [{'key': 'a', 'value': 99, 'children': [{'key': 'x', 'value': 1, 'children': []}, {'key': 'y', 'value': 2, 'children': []}]}]})

Output: 7

Example 4

Input: count_tree_changes({'key': 'old', 'value': 0, 'children': [{'key': 'a', 'value': 1, 'children': []}]}, {'key': 'new', 'value': 0, 'children': [{'key': 'b', 'value': 2, 'children': []}]})

Output: 4

Hints

Hint 1

Think of the traversal as walking pairs of nodes that sit at the same key path in both trees. A pair can also have one side missing.

Hint 2

When a pair is missing one side, or the values (or root keys) differ, you add subtree sizes and stop descending. Only when the values match do you go on to the children.

Hint 3

Index each node's children by key in a dictionary, then look at the union of keys. Use an explicit stack and compute subtree sizes iteratively so deep chains don't overflow recursion.