DevPrep

All topics › Algorithms & data structures

What is a tree and a binary search tree (BST)?

Middle PRO

Tree

A connected acyclic graph: one root, every node has children, leaf nodes have none. iOS examples: the view hierarchy, the DOM, the file system, an AST, NavigationStack/UINavigationController as a path through a tree of screens. In a binary tree a node has at most two children.

Traversals: depth-first (DFS: pre-order, in-order, post-order — recursion or an explicit stack) and breadth-first (BFS — a queue, level by level).

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store