KatInterview

Validate Binary Tree from Directed Edge List

Trees & GraphsAuto-graded · 24 test cases
MediumAuto-gradedTrees & Graphstreesgraphsdfs

Last updated

Evaluates tree properties, cycle detection, edge validity, and in-order traversal under strict error precedence.

Given a list of directed edges [parent, child] using positive integer node identifiers, determine if the graph forms a single valid binary tree. The set of nodes in the graph is defined as all unique node IDs appearing in the input edge list.

A valid binary tree satisfies all of the following conditions:

  1. There is exactly one root node (a node with no incoming parent edges).
  2. Every non-root node has exactly one parent node.
  3. No node has more than two child nodes.
  4. The graph contains no cycles and is fully connected (all nodes are reachable from the single root node).

Left/Right Child Ordering Rules

The edges appear in a given fixed order:

  • The first directed edge originating from a parent node points to its left child.
  • The second directed edge originating from a parent node points to its right child.
  • If a parent node has only one child edge, that child is treated as its left child.

Verification Priority Rules

If the graph fails to form a single valid binary tree, you must identify and return the first applicable error code from the following ordered hierarchy:

  1. INVALID_INPUT — An edge is a self-loop (where parent == child) OR an exact identical directed edge [parent, child] appears multiple times.
  2. TOO_MANY_CHILDREN — A parent node has more than two child edges.
  3. MULTIPLE_PARENTS — A child node has more than one parent edge.
  4. CYCLE — The graph contains at least one directed cycle.
  5. MULTIPLE_ROOTS — There are two or more nodes that have no incoming parent edges (a forest of disjoint components).

If all checks pass and the edges form a valid binary tree, output the node values as a space-separated string corresponding to the tree's in-order traversal (left subtree, current node, right subtree).

Input Format

  • edges: A list of 2-element integer lists [parent, child].

Output Format

  • Return a single string: either the exact error string (e.g., "CYCLE") or space-separated node labels representing the in-order traversal (e.g., "4 2 1 5 3").

Constraints

  • 1 <= len(edges) <= 10^4
  • 1 <= node label <= 10^5
  • Input errors at higher priority must be reported even if lower-priority errors exist further along in processing.

Examples

Example 1

Input: validate_binary_tree([[1, 2], [1, 3], [2, 4], [3, 5]])

Output: '4 2 1 5 3'

Example 2

Input: validate_binary_tree([[1, 2], [2, 3], [3, 1]])

Output: 'CYCLE'

Example 3

Input: validate_binary_tree([[1, 2], [1, 3], [1, 4]])

Output: 'TOO_MANY_CHILDREN'

Example 4

Input: validate_binary_tree([[1, 2], [3, 4]])

Output: 'MULTIPLE_ROOTS'

Example 5

Input: validate_binary_tree([[1, 2], [1, 2]])

Output: 'INVALID_INPUT'

Example 6

Input: validate_binary_tree([[1, 2], [1, 3], [2, 4], [3, 4]])

Output: 'MULTIPLE_PARENTS'

Hints

Hint 1

Strictly follow the error hierarchy: perform checks sequentially from step 1 to step 5.

Hint 2

When recording children for a parent node, maintain the original sequence of edges to preserve left and right subtree assignments.

Hint 3

Use a 3-color state iterative DFS (0=unvisited, 1=visiting, 2=visited) to safely detect directed cycles without stack overflow.