Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: Tree
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | p_id | int | +-------------+------+ 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:
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: 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 | +----+-------+ 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: Tree table: +----+------+ | id | p_id | +----+------+ | 1 | null | +----+------+ Output: +----+-------+ | id | type | +----+-------+ | 1 | Root | +----+-------+ Explanation: If there is only one node on the tree, you only need to output its root attributes.
Note: This question is the same as 3054: Binary Tree Nodes.
Problem summary: Table: Tree +-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | p_id | int | +-------------+------+ 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"Tree":["id","p_id"]},"rows":{"Tree":[[1,null],[2,1],[3,1],[4,2],[5,2]]}}{"headers":{"Tree":["id","p_id"]},"rows":{"Tree":[[1,null]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #608: Tree Node
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #608: Tree Node
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #608: Tree Node
// # Write your MySQL query statement below
// SELECT
// id,
// CASE
// WHEN p_id IS NULL THEN 'Root'
// WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
// ELSE 'Leaf'
// END AS type
// FROM Tree;
// "#
// }
// Accepted solution for LeetCode #608: Tree Node
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #608: Tree Node
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #608: Tree Node
// # Write your MySQL query statement below
// SELECT
// id,
// CASE
// WHEN p_id IS NULL THEN 'Root'
// WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
// ELSE 'Leaf'
// END AS type
// FROM Tree;
// "#
// }
# Accepted solution for LeetCode #608: Tree Node
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #608: Tree Node
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #608: Tree Node
# # Write your MySQL query statement below
# SELECT
# id,
# CASE
# WHEN p_id IS NULL THEN 'Root'
# WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
# ELSE 'Leaf'
# END AS type
# FROM Tree;
# "#
# }
// Accepted solution for LeetCode #608: Tree Node
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #608: Tree Node
# Write your MySQL query statement below
SELECT
id,
CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
ELSE 'Leaf'
END AS type
FROM Tree;
"#
}
// Accepted solution for LeetCode #608: Tree Node
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #608: Tree Node
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #608: Tree Node
// # Write your MySQL query statement below
// SELECT
// id,
// CASE
// WHEN p_id IS NULL THEN 'Root'
// WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
// ELSE 'Leaf'
// END AS type
// FROM Tree;
// "#
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.