Sponsored
Use these hints if you're stuck. Try solving on your own first.
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.
Solve with full IDE support and test cases