How to make single parent of all the Childs and their child ?

Hi all,
Do any of you know how to can i make a single parent obj and that obj will consist of only child with no further child.
And if child have another child detach those child and make them child of the root obj.
here is the image for more detailed understanding.

I know i can do this by recursive checking and then so on… . . but let me know if you some some sort of in built method or any shortcut.

Transform allChildren = root.GetComponentsInChildren();
foreach(var child in allChildren) {
if(child != root)
child.parent = root;

Where root is the transform of the root object.