Skip to content

Practice Tree Node

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#608
LeetCode Problem

Tree Node

Problem Statement

Tables: Tree(id INT, p_id INT). Tree.id is unique. p_id identifies a parent; the data forms a valid tree. Return id and type for every node. A node without a parent is Root, even if it has no children. A non-root with no children is Leaf; every other node is Inner. Output order is unrestricted.

Source: Tree Node on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
Tree table: +----+------+ | id | p_id | +----+------+ | 1  | null | | 2  | 1    | | 3  | 1    | | 4  | 2    | | 5  | 2    | +----+------+
Output
+----+-------+ | id | type  | +----+-------+ | 1  | Root  | | 2  | Inner | | 3  | Leaf  | | 4  | Leaf  | | 5  | Leaf  | +----+-------+

# Example 2

Input
Tree table: +----+------+ | id | p_id | +----+------+ | 1  | null | +----+------+
Output
+----+-------+ | id | type  | +----+-------+ | 1  | Root  | +----+-------+

Related LeetCode Problems

Practice more problems similar to Tree Node.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.