Find Root IDs from Parent References
Problem statement
Each row in nodes represents one JSON-like object as [id, parentId]. Every id is unique. An empty parentId represents null.
A node is a root when its parentId is empty or when that parent ID does not appear among the supplied node IDs. Return all root IDs in lexicographic order.
Function
findRootIds(nodes: String[][]) → String[]Examples
Example 1
nodes = [["a",""],["b","a"],["c","a"],["d","missing"]]return = ["a","d"]Node a has no parent, and d references a parent that is absent. Nodes b and c reference a.
Example 2
nodes = [["child","root"],["root",""]]return = ["root"]Input order does not matter; root is the only node without a present parent.
Example 3
nodes = [["x","y"],["y","x"]]return = []Both parent references are present, so this cycle contains no root under the stated rule.
Constraints
0 <= nodes.length <= 100000.nodes[i].length == 2.- Each
idis a distinct non-empty string. - Each
parentIdis either empty or a non-empty string. - The combined length of all strings is at most
10^6.