# Tree Node LeetCode Solution

This Leetcode problem Tree Node LeetCode Solution is done in SQL.

# List of all LeetCode Solution

## Problem Statement

id is the column with unique values for this table. Each row of this table contains information about the id of a node and the id of its parent node in a tree. The given structure is always a valid tree.

Each node in the tree can be one of three types:

• “Leaf”: if the node is a leaf node.
• “Root”: if the node is the root of the tree.
• “Inner”: If the node is neither a leaf node nor a root node.

Write a solution to report the type of each node in the tree.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:

Output:

Explanation:
Node 1 is the root node because its parent node is null and it has child nodes 2 and 3.
Node 2 is an inner node because it has parent node 1 and child node 4 and 5.
Nodes 3, 4, and 5 are leaf nodes because they have parent nodes and they do not have child nodes.

Example 2:

Input:

Output:

Explanation: If there is only one node on the tree, you only need to output its root attributes.

## Tree Node LeetCode Solution MySQL

``````select
id,
case when p_id is null then 'Root' when p_id is not null
and id in (
select
distinct p_id
from
tree
) then 'Inner' else 'Leaf' end as Type
from
tree;```Code language: SQL (Structured Query Language) (sql)```
Scroll to Top