FastPrepDecrypt a String from Binary Tree Columns

Decrypt a String from Binary Tree Columns

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

A binary tree stores one lowercase English letter at every node. The tree is represented by parallel arrays:

  • values[i] is node i's one-letter string.
  • leftIndices[i] and rightIndices[i] are its child indices, or -1 when that child is absent.
  • rootIndex is the root, or -1 for an empty tree.

Assign the root to column 0. A left child is one column left of its parent, and a right child is one column right. Read occupied columns from leftmost to rightmost. Within each column, read nodes from top to bottom; nodes with the same row and column keep their left-to-right breadth-first encounter order.

Concatenate the letters in that order and return the decrypted string.

Function

decryptTreeColumns(values: String[], leftIndices: int[], rightIndices: int[], rootIndex: int) → String

Examples

Example 1

values = ["i","r","e","p","c"]leftIndices = [1,3,4,-1,-1]rightIndices = [2,-1,-1,-1,-1]rootIndex = 0return = "price"

The columns are [p], [r], [i,c], and [e]. Node c is the left child of e, so it shares the root's column.

Example 2

values = ["a","b","c","d","e","f","g"]leftIndices = [1,3,5,-1,-1,-1,-1]rightIndices = [2,4,6,-1,-1,-1,-1]rootIndex = 0return = "dbaefcg"

Nodes e and f share row and column, so breadth-first encounter order places e before f.

Example 3

values = []leftIndices = []rightIndices = []rootIndex = -1return = ""

An empty tree decrypts to the empty string.

Constraints

  • 0 <= values.length <= 100000.
  • The three arrays have equal length.
  • Every values[i] contains exactly one lowercase English letter.
  • Every child index is -1 or a valid array index.
  • When nonempty, the arrays describe one valid rooted binary tree and every node is reachable exactly once from rootIndex.
  • rootIndex == -1 exactly when the arrays are empty.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String decryptTreeColumns(String[] values, int[] leftIndices, int[] rightIndices, int rootIndex) {
    // Write your code here.
}
values["i","r","e","p","c"]
leftIndices[1,3,4,-1,-1]
rightIndices[2,-1,-1,-1,-1]
rootIndex0
expected"price"
Checking account…