{ "data": { "question": { "questionId": "608", "questionFrontendId": "608", "categoryTitle": "Database", "boundTopicId": 2637, "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 column with unique values 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 a solution to report the type of each node in the tree.
\n\nReturn the result table in any order.
\n\nThe 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": "树节点", "translatedContent": "
表:Tree
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| id | int |\n| p_id | int |\n+-------------+------+\nid 是该表中具有唯一值的列。\n该表的每行包含树中节点的 id 及其父节点的 id 信息。\n给定的结构总是一个有效的树。\n\n\n
\n\n
树中的每个节点可以是以下三种类型之一:
\n\n编写一个解决方案来报告树中每个节点的类型。
\n\n以 任意顺序 返回结果表。
\n\n结果格式如下所示。
\n\n\n\n
示例 1:
\n\n\n输入:\nTree table:\n+----+------+\n| id | p_id |\n+----+------+\n| 1 | null |\n| 2 | 1 |\n| 3 | 1 |\n| 4 | 2 |\n| 5 | 2 |\n+----+------+\n输出:\n+----+-------+\n| id | type |\n+----+-------+\n| 1 | Root |\n| 2 | Inner |\n| 3 | Leaf |\n| 4 | Leaf |\n| 5 | Leaf |\n+----+-------+\n解释:\n节点 1 是根节点,因为它的父节点为空,并且它有子节点 2 和 3。\n节点 2 是一个内部节点,因为它有父节点 1 和子节点 4 和 5。\n节点 3、4 和 5 是叶子节点,因为它们有父节点而没有子节点。\n\n\n
示例 2:
\n\n\n输入:\nTree table:\n+----+------+\n| id | p_id |\n+----+------+\n| 1 | null |\n+----+------+\n输出:\n+----+-------+\n| id | type |\n+----+-------+\n| 1 | Root |\n+----+-------+\n解释:如果树中只有一个节点,则只需要输出其根属性。\n\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 209, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef tree_node(tree: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"62.1K\", \"totalSubmission\": \"100.8K\", \"totalAcceptedRaw\": 62123, \"totalSubmissionRaw\": 100846, \"acRate\": \"61.6%\"}", "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": null, "status": null, "sampleTestCase": "{\"headers\":{\"Tree\":[\"id\",\"p_id\"]},\"rows\":{\"Tree\":[[1,null],[2,1],[3,1],[4,2],[5,2]]}}", "metaData": "{\"mysql\":[\"Create table If Not Exists Tree (id int, p_id int)\"],\"mssql\":[\"Create table Tree (id int, p_id int)\"],\"oraclesql\":[\"Create table Tree (id int, p_id int)\"],\"database\":true,\"name\":\"tree_node\",\"pythondata\":[\"Tree = pd.DataFrame([], columns=['id', 'p_id']).astype({'id':'Int64', 'p_id':'Int64'})\"],\"postgresql\":[\"\\nCreate table If Not Exists Tree (id int, p_id int)\"],\"database_schema\":{\"Tree\":{\"id\":\"INT\",\"p_id\":\"INT\"}}}", "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, "envInfo": "{\"mysql\":[\"MySQL\",\"
\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"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]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"