Intuition
We are given an array with descriptions of nodes (parent and child relation). We have to construct a binary tree based on this information.
One way to solve it, is take a node and check if there exists it’s parent/child in the list. But this would be too inefficient, worst case would be .
So instead we use a map to represent the tree. Finally, to find the root of the tree, we also maintain a set with only the children. i
Then we subtract all the keys in the map (tree representation) and this children set, to find the root of the tree.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is where
N == len(descriptions)
-
Memory
The memory usage is since we use a hashmap (python dict) to store parent and children nodes.
— A