Build and Render an Organization Role Tree
Problem statement
Build a company’s role tree from relations given in any order, then return its text representation.
Each relation has the form role,parent. Exactly one relation has an empty parent and defines the root. Every other parent names another role in the input.
Visit each role before its children. Visit each child’s entire subtree before moving to the next child, using the sorted child order described below. Write each role as:
<two hyphens repeated depth times><role>The root has depth 0. Sort every role's direct children in ascending lexicographic order before traversal.
Function
renderOrganization(relations: String[]) → String[]Examples
Example 1
relations = ["engineer,manager","ceo,","manager,ceo","designer,manager","finance,ceo"]return = ["ceo","--finance","--manager","----designer","----engineer"]The root's children are sorted as finance then manager. The manager's children are sorted as designer then engineer.
Example 2
relations = ["intern,engineer","engineer,lead","lead,"]return = ["lead","--engineer","----intern"]The unordered relations form one chain with depths 0, 1, and 2.
Constraints
1 <= relations.length <= 10^5.- Every role is unique and contains lowercase English letters, digits, or underscores.
- Exactly one role has an empty parent.
- Every nonroot parent exists, and the relations form one valid tree.
- Relations may arrive in any order.