Decrypt a String from Binary Tree Columns
Problem statement
A binary tree stores one lowercase English letter at every node. The tree is represented by parallel arrays:
values[i]is nodei's one-letter string.leftIndices[i]andrightIndices[i]are its child indices, or-1when that child is absent.rootIndexis the root, or-1for 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) → StringExamples
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
-1or a valid array index. - When nonempty, the arrays describe one valid rooted binary tree and every node is reachable exactly once from
rootIndex. rootIndex == -1exactly when the arrays are empty.