LearnThatStack Ace your next interview
Trees, BSTs & Heaps · question
Question 1 of 76

What is a tree, and what do root, parent, child, and leaf mean?

beginner
← All Trees, BSTs & Heaps questions
Re-explain

A tree is a hierarchical structure of nodes connected by edges, with no cycles. Each node holds a value and links to nodes below it. One node sits at the top as the root, the single entry point with no parent.

Parent and child describe a direct link. The node above is the parent, and the nodes hanging off it are its children. A leaf is any node with no children. Internal nodes have at least one child.

You reach every other node by following edges down from the root. Trees model nested containment, such as file systems, HTML documents and org charts. Each step down a search commits to one branch and drops the rest, which is why trees beat flat lists for hierarchical data.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Trees, BSTs & Heaps One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • How is a tree different from a general graph? A graph may have cycles, several paths between nodes, or disconnected parts. A tree is connected with no cycles, so exactly one path joins any two nodes.
  • What is the difference between a node's depth and its height? Depth counts edges up to the root, height counts edges down to the deepest leaf below it. The root has depth 0 and every leaf has height 0.
  • A tree has n nodes. How many edges does it have, and why? n - 1. Every node except the root has exactly one edge to its parent, and the root has none.
  • What is a binary tree, and how does it differ from a general tree? Each node has at most two children, called left and right. A general tree puts no limit on how many children a node has.
  • How would you visit every node in a tree? Name depth-first orders using recursion or a stack, and level-order using a queue. Each visits every node once, so O(n) time.

What you can say

  1. A tree is a hierarchy of nodes joined by edges, with no cycles, where each node holds a value and links to the nodes below it.
  2. At the top is the root, the one node with no parent, and it's the entry point, so you reach every other node by following edges down from it.
  3. Parent and child describe a direct link. The node above is the parent, and the nodes hanging off it are its children.
  4. A leaf is any node with no children, and a node with at least one child is an internal node.
  5. Trees model nested containment, like file systems, HTML documents and org charts, and each step down commits to one branch and drops the rest.
  6. That's why trees beat flat lists for hierarchical data. The catch is one parent per node and no cycles, so data where an item has two parents needs a graph instead.

Weak answers to avoid

  • Leaves out the no-cycles rule Without it you've described a graph. State that there are no cycles and every node but the root has one parent, so one path leads from the root to each node.
  • Assumes every tree is binary Two children is the binary-tree limit, not part of the definition. A general tree node can have any number of children, so define that first.
  • Says leaves are the nodes on the bottom level Leaves can sit at different depths. A leaf is any node with no children, wherever it is, and every node with a child is internal.
  • Recites the terms with no example or reason Interviewers want to know why trees exist. Map the terms onto a file system, with files as leaves, and say how each step down skips whole branches.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Trees, BSTs & Heaps cheatsheet.

← Back to all Trees, BSTs & Heaps questions
Pro · $10/mo

65 of 76 Trees, BSTs & Heaps answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime