Fast and Simple Solution with description
Problem Statement\nGiven a table Tree with columns id and p_id representing a tree structure, the task is to classify each node as either \'Root,\' \'Inner,\' o
# Problem Statement\nGiven a table `Tree` with columns `id` and `p_id` representing a tree structure, the task is to classify each node as either \'Root,\' \'Inner,\' or \'Leaf\' based on its relationship with the parent node.\n\n## Intuition\nThe problem involves classifying each node in the tree based on whether it is a root, an inner node, or a leaf.\n\n## Approach\n1. **Use of `CASE` Statement:**\n   - Utilize the `CASE` statement to classify each node based on its relationship with the parent node (`p_id`).\n   - If a node has a `NULL` value for `p_id`, it is classified as \'Root.\'\n   - If a node\'s `id` appears in the list of `p_id` values in the `Tree` table, it is classified as \'Inner.\'\n   - Otherwise, the node is classified as \'Leaf.\'\n\n2. **Result Output:**\n   - Select the `id` of each node along with its corresponding type (\'Root,\' \'Inner,\' or \'Leaf\').\n\n## Complexity\n- **Time complexity:**\n  - The time complexity is primarily determined by the use of the `CASE` statement and the subquery. It is generally efficient and can be considered as $$O(n)$$, where $$n$$ is the total number of rows in the `Tree` table.\n\n- **Space complexity:**\n  - The space complexity is $$O(1)$$. The algorithm uses a constant amount of additional space regardless of the input size.\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT id,\n    (CASE \n        WHEN p_id IS NULL THEN \'Root\'\n        WHEN id IN (SELECT p_id FROM Tree) THEN \'Inner\'\n        ELSE \'Leaf\'\n    END) AS type\nFROM Tree;\n```\n![image.png](https://assets.leetcode.com/users/images/212a3bfc-fe1c-44cd-bf25-7a10a468dfc7_1703333678.63116.png)\n
['Database', 'MySQL']