22. Node Classification

Hierarchical structures like file systems, organizational charts, or comment threads in a forum often need to differentiate between root, inner, and leaf nodes. Proper classification helps in rendering or processing the hierarchy efficiently.

You are given a table Tree:

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘     id      ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘    p_id     ā•‘   int    ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • id: The unique ID of a node (Primary Key).
  • p_id: The ID of the parent node for this node.
  • A node where p_id IS NULL represents the root of the tree (since it has no parent).
  • The table forms a valid tree (i.e., it is connected and acyclic with one root).

Each node can be classified as:

╔═══════╦═════════════════════════════════════════════════════════════════╗
ā•‘ Type  ā•‘                           Description                           ā•‘
╠═══════╬═════════════════════════════════════════════════════════════════╣
ā•‘ Root  ā•‘ If p_id IS NULL (i.e., no parent)                               ā•‘
║───────┼─────────────────────────────────────────────────────────────────║
ā•‘ Leaf  ā•‘ If the node does not appear as a parent of any other node       ā•‘
║───────┼─────────────────────────────────────────────────────────────────║
ā•‘ Inner ā•‘ If the node has a parent (p_id is not NULL) and is also a       ā•‘
ā•‘       ā•‘ parent to at least one other node                               ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Write a query to determine the type of each node in a tree structure. Each node should be labeled as one of the following:

  • 'Root': if it has no parent
  • 'Leaf': if it has no children
  • 'Inner': if it has both a parent and children

The output can be in any order.

Example 1:

Example 1:

Input:

╔════╦══════╗
ā•‘ id ā•‘ p_id ā•‘
╠════╬══════╣
ā•‘ 10 ā•‘ null ā•‘
ā•šā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•

Expected Output:

╔════╦══════╗
ā•‘ id ā•‘ type ā•‘
╠════╬══════╣
ā•‘ 10 ā•‘ Root ā•‘
ā•šā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•

Explanation:

Node 10: p_id is null = Root

Example 2:

Example 2:

Input:

╔════╦══════╗
ā•‘ id ā•‘ p_id ā•‘
╠════╬══════╣
ā•‘ 1  ā•‘ null ā•‘
║────┼──────║
ā•‘ 2  ā•‘  1   ā•‘
║────┼──────║
ā•‘ 3  ā•‘  1   ā•‘
║────┼──────║
ā•‘ 4  ā•‘  2   ā•‘
║────┼──────║
ā•‘ 5  ā•‘  2   ā•‘
ā•šā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•

Expected Output:

╔════╦═══════╗
ā•‘ id ā•‘ type  ā•‘
╠════╬═══════╣
ā•‘ 1  ā•‘ Root  ā•‘
║────┼───────║
ā•‘ 2  ā•‘ Inner ā•‘
║────┼───────║
ā•‘ 3  ā•‘ Leaf  ā•‘
║────┼───────║
ā•‘ 4  ā•‘ Leaf  ā•‘
║────┼───────║
ā•‘ 5  ā•‘ Leaf  ā•‘
ā•šā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•

Explanation:

  • Node 1: p_id is null → Root
  • Node 2: Has a parent (1) and children (4,5) → Inner
  • Node 3: Has a parent (1) but no children → Leaf
  • Node 4 & 5: Have parents (2) but no children → Leaf

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

Tree