Given multiple tuples in the form of (A,B) where A is the parent and B is the child in a binary tree, find if the input is valid or not. 4 error conditions were provided:
- If a parent has more than 2 children,
- If duplicate tuples entered,
- If the tree has a cycle,
- If more than one root possible.
For violation of multiple validity conditions, print the condition coming first in the above order. If the input is valid, print the tree in a serial representation. For eg: If input is (A,B), (B,C), (A,D), (C,E) , output: (A(B(C(E)))(D))
I am thinking of solving it through union-find data structure but not able to code it. Can anyone help me with the logic or pseudo code in c/c++