A build system keeps a flat table of directories. Every directory has a unique id from 0 to n - 1. Every directory except one records the id of its parent directory; exactly one directory has no parent at all and sits at the top of the tree. Classify every directory into exactly one of three roles:
Root -- the one directory with no parent.Leaf -- a directory that owns no subdirectories (and is not the root).Branch -- any other directory: it has a parent and owns at least one subdirectory.Given the parent pointers, report the role of every directory.
The first line contains an integer n.
The second line contains n integers parent_0 parent_1 ... parent_{n-1}, where parent_i is the id of directory i's parent, or -1 if directory i is the root.
Print n lines, one per directory, ordered by increasing id: <id> <role> where <role> is one of Root, Leaf, Branch.
parent_i = -1.parent_i is a valid directory id (0 <= parent_i < n, parent_i != i), and the parent pointers form a single tree rooted at the -1 directory (no cycles, every directory reachable from the root).Example 1
Input
5 -1 0 0 1 1
Expected
0 Root 1 Branch 2 Leaf 3 Leaf 4 Leaf
Explanation
Directory 0 has parent -1, so it is the Root. Directories 3 and 4 both list directory 1 as their parent, so directory 1 owns subdirectories and is a Branch. Directory 2's parent is 0 but no directory lists 2 as a parent, so it is a Leaf. Directories 3 and 4 each have parent 1 and no children of their own, so they are Leaf as well.
Example 2
Input
1 -1
Expected
0 Root
Explanation
There is only one directory, and its parent is -1. Even though it also has no subdirectories, the Root check takes priority over the Leaf check, so it is classified as Root, not Leaf.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →