{ "data": { "question": { "questionId": "608", "questionFrontendId": "608", "boundTopicId": null, "title": "Tree Node", "titleSlug": "tree-node", "content": "
Table: Tree
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| id | int |\n| p_id | int |\n+-------------+------+\nid is the primary key column for this table.\nEach row of this table contains information about the id of a node and the id of its parent node in a tree.\nThe given structure is always a valid tree.\n\n\n
\n\n
Each node in the tree can be one of three types:
\n\nWrite an SQL query to report the type of each node in the tree.
\n\nReturn the result table ordered by id
in ascending order.
The query result format is in the following example.
\n\n\n
Example 1:
\n\n\nInput: \nTree table:\n+----+------+\n| id | p_id |\n+----+------+\n| 1 | null |\n| 2 | 1 |\n| 3 | 1 |\n| 4 | 2 |\n| 5 | 2 |\n+----+------+\nOutput: \n+----+-------+\n| id | type |\n+----+-------+\n| 1 | Root |\n| 2 | Inner |\n| 3 | Leaf |\n| 4 | Leaf |\n| 5 | Leaf |\n+----+-------+\nExplanation: \nNode 1 is the root node because its parent node is null and it has child nodes 2 and 3.\nNode 2 is an inner node because it has parent node 1 and child node 4 and 5.\nNodes 3, 4, and 5 are leaf nodes because they have parent nodes and they do not have child nodes.\n\n\n
Example 2:
\n\n\nInput: \nTree table:\n+----+------+\n| id | p_id |\n+----+------+\n| 1 | null |\n+----+------+\nOutput: \n+----+-------+\n| id | type |\n+----+-------+\n| 1 | Root |\n+----+-------+\nExplanation: If there is only one node on the tree, you only need to output its root attributes.\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 381, "dislikes": 32, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "{\"headers\": {\"Tree\": [\"id\", \"p_id\"]}, \"rows\": {\"Tree\": [[1,null],[2,1],[3,1],[4,2],[5,2]]}}\n{\"headers\": {\"Tree\": [\"id\", \"p_id\"]}, \"rows\": {\"Tree\": [[1,null]]}}", "categoryTitle": "Database", "contributors": [], "topicTags": [ { "name": "Database", "slug": "database", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below\n", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"46.4K\", \"totalSubmission\": \"64.3K\", \"totalAcceptedRaw\": 46436, \"totalSubmissionRaw\": 64263, \"acRate\": \"72.3%\"}", "hints": [ "You can judge the node type by querying whether the node's id shows up in p_id column and whether the node's p_id is null." ], "solution": { "id": "201", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "{\"headers\": {\"Tree\": [\"id\", \"p_id\"]}, \"rows\": {\"Tree\": [[1,null],[2,1],[3,1],[4,2],[5,2]]}}", "metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Tree (id int, p_id int)\"\n ],\n \"mssql\": [\n \"Create table Tree (id int, p_id int)\"\n ],\n \"oraclesql\": [\n \"Create table Tree (id int, p_id int)\"\n ],\n \"database\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Tree (id int, p_id int)", "Truncate table Tree", "insert into Tree (id, p_id) values ('1', 'None')", "insert into Tree (id, p_id) values ('2', '1')", "insert into Tree (id, p_id) values ('3', '1')", "insert into Tree (id, p_id) values ('4', '2')", "insert into Tree (id, p_id) values ('5', '2')" ], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"
MySQL 8.0
.
mssql server 2019
.
Oracle Sql 11.2
.