Back to blog
November 10, 20255 min readMarina

This is a Tree or a Graph?

AlgorithmsData StructuresGraphs

This is a Tree or a Graph?

Another day I was taking an AI course, and the algorithm for a search problem example didn’t look like the typical "network-style" graph. It looked much more like a tree, and that’s a great moment to ask a classic interview question:

"Is this a tree, or is this a graph?"

It matters because every tree is a graph, but not every graph is a tree.

Tree

A tree is a specialized graph, with:

  • one root
  • no cycles
  • one unique path between nodes
  • a clear hierarchy
  • leaves: nodes with no children, the endpoints of a tree
  • Common uses: decision trees, file systems, org charts, binary search trees.

    Graph

    A graph is more flexible:

  • may have cycles
  • multiple paths
  • no root required
  • models general relationships
  • Common uses: social networks, transportation networks, knowledge graphs, recommendation systems.

    Rule of thumb

    In interviews and real-world systems, the rule of thumb is simple:

  • If it’s hierarchical → tree.
  • If it’s relational → graph.
  • When in doubt, remember: a tree is a graph with rules.