Beginner friendly MySQL Solution👍🏻| Tree Node using CASE & Subqueries🌀
\uD83D\uDCA1Intuition\nThe problem requires classifying nodes in a tree structure into three types: \'Root\', \'Inner\', and \'Leaf\'. Each node can be a root i
# \uD83D\uDCA1Intuition\nThe problem requires classifying nodes in a tree structure into three types: \'Root\', \'Inner\', and \'Leaf\'. Each node can be a root if it does not have a parent, an inner node if it has children, or a leaf if it does not have any children.\n\n\n\n# \uD83C\uDFAFApproach\n**Step 1:** Identify root nodes by checking if a node does not have a parent (p_id is NULL).\n**Step 2:** Identify inner nodes by checking if the node ID exists as a parent ID (p_id) in the tree.\n**Step 3:** Identify leaf nodes as those that are neither root nor inner nodes.\n**Step 4:** Use a CASE statement to classify each node based on the above conditions.\n\n# \uD83E\uDD2FComplexity\n- Time complexity:\nO(n 2)\n\n- Space complexity:\nO(1)\n\n# \uD83D\uDCC3Code\n```\n# Write your MySQL query statement below\n\n\nselect id,\ncase 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```
['MySQL']