Stack Exchange
Neo4j / Cypher - How to retrieve categories with AND without subcategories
Using the example here, the following query will return all categories along with their subcategories:
MATCH p=(n:Category)-[:subcategory*]->(m)
WHERE NOT ()-[:subcategory]->(n)
WITH COLLECT(p) AS ps
CALL apoc.convert.toTree(ps) yield value
RETURN value;

How can the query be modified to also get categories without subcategories?

A minor tweak should do it:
MATCH p=(n:Category)-[:subcategory*0..]->(m)
WHERE NOT ()-[:subcategory]->(n)
WITH COLLECT(p) AS ps
CALL apoc.convert.toTree(ps) YIELD value
RETURN value;

The *0.. pattern includes 0-length paths (i.e., paths with no subcategories).
