KatInterview

Anticlockwise Perimeter Walk of an Index-Based Binary Tree

Trees & GraphsAuto-graded · 20 test cases
MediumAuto-gradedTrees & Graphsbinary-treetree-traversaldfs

Last updated

Compute the anticlockwise perimeter of a binary tree given as child-index arrays, combining root, left edge, leaves and right edge without duplicates.

You are given a binary tree stored in three parallel arrays. Produce the values on its outer perimeter, listed in anticlockwise order starting at the root.

Input format

  • The tree has n nodes labelled 0 to n - 1, and node 0 is the root.
  • values[i] is the value stored in node i.
  • left[i] and right[i] are the labels of node i's left and right children, or -1 if that child does not exist.

Definitions

  • A leaf is a node with no children. The root is never counted as a leaf, even if it has no children.
  • The left edge is built as follows. Start at the root's left child. Repeatedly step to the current node's left child if it exists, otherwise to its right child, until you arrive at a leaf. The left edge consists of all nodes visited except that final leaf. If the root has no left child, the left edge is empty.
  • The right edge is the mirror image. Start at the root's right child. Repeatedly step to the current node's right child if it exists, otherwise to its left child, until you arrive at a leaf. The right edge consists of all nodes visited except that final leaf. If the root has no right child, the right edge is empty.
  • The leaf sequence contains every leaf of the tree, in the order a left-to-right depth-first traversal encounters them.

Task

Return a list of values made by concatenating, in this order:

  1. the root's value,
  2. the left edge's values, from top to bottom,
  3. the leaf sequence's values,
  4. the right edge's values, from bottom to top.

Every node appears at most once in the result.

Constraints

  • 1 <= n <= 10^4, and len(values) == len(left) == len(right) == n
  • -1000 <= values[i] <= 1000
  • Every child index is -1 or between 1 and n - 1; every node other than 0 is the child of exactly one node; the links form a single binary tree rooted at node 0.
  • The tree may be a single path, so its depth can be as large as n.

Examples

Example 1

Input: perimeter_values([1, 2, 3, 4], [-1, 2, -1, -1], [1, 3, -1, -1])

Output: [1, 3, 4, 2]

Example 2

Input: perimeter_values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 3, 5, -1, 6, 8, -1, -1, -1, -1], [2, 4, -1, -1, 7, 9, -1, -1, -1, -1])

Output: [1, 2, 4, 7, 8, 9, 10, 6, 3]

Example 3

Input: perimeter_values([5], [-1], [-1])

Output: [5]

Hints

Hint 1

Split the answer into four independent pieces: the root, the left edge, the leaves, and the right edge. Decide for each piece exactly which nodes it is allowed to take, so nothing is duplicated.

Hint 2

When walking an edge, prefer the outer child but fall back to the inner child if the outer one is missing. Stop before adding the leaf you land on, since leaves are handled by the leaf pass.

Hint 3

Depth can reach 10^4, so avoid recursion. Use an explicit stack for the leaf pass, pushing the right child before the left so leaves come out left to right, and skip that pass when the root itself has no children.