Easy Solution (using Left Join)
\n# Intuition\nImagine you have a table that represents a tree, where each row corresponds to a node in the tree. Each node has an ID (a unique identifier) and 
\n# Intuition\nImagine you have a table that represents a tree, where each row corresponds to a node in the tree. Each node has an ID (a unique identifier) and a Parent ID (the identifier of its parent node, if any). We want to figure out what type each node is in the tree - whether it\'s a Root (has no parent), an Inner node (has a parent and may have children), or a Leaf node (has a parent but no children).\n\n# Approach\nTo determine the type of each node in the tree, we use a self-join on the table. Self-join means we treat the table as two copies and compare nodes against each other.\n\n1. For each node, we check if it has a Parent ID (P_ID). If it doesn\'t have a Parent ID (P_ID is blank), it means the node has no parent, so it\'s a Root node. We label such nodes as \'Root\'.\n\n1. If the node has a Parent ID (P_ID), we look for a corresponding node in the table that has its ID as the Parent ID. If we find a match, it means the node has a parent, so it\'s an Inner node. We label such nodes as \'Inner\'.\n\n1. If the node doesn\'t match any other node\'s Parent ID, it means it has no children, so it\'s a Leaf node. We label such nodes as \'Leaf\'.\n\n# Complexity\n- Time complexity:\nThe time complexity of the SQL query is $$O(n^2)$$, where n is the number of rows in the Tree table. This is because the query involves a self-join, and for each row in the original table, it searches through the entire table again to find matching parent-child relationships.\n\n\n- Space complexity:\nThe space complexity of the SQL query is $$O(n)$$, where n is the number of rows in the Tree table. This is because the query creates two virtual copies of the Tree table (t and p) through the self-join, and both copies have the same number of rows as the original table.\n\n# Code\n```\n# Write your MySQL query statement below\n\nSELECT \n    DISTINCT t.id,\n    IF(t.p_id IS NULL, \'Root\', IF(t.id = p.p_id, \'Inner\', \'Leaf\')) AS type\nFROM \n    Tree t\nLEFT JOIN \n    Tree p ON t.id = p.p_id;\n\n```\n\n\n
['Database', 'MySQL']