Skip to main content

Trees

3 lessonsAbout 18 minutesIntermediate

This part of the DSA Tutorial runs from Binary Trees Explained through to Common Tree Problems. There are 3 lessons here, and working through them takes about 18 minutes at a steady pace.

It follows on from Heaps and Priority Queues, so finish that first if you have not already — the examples below assume you are comfortable with it.

Each lesson below says what it covers before you open it. Read them in order the first time; afterwards this page works as a index you can jump back into when you need to check one thing.

  1. 1Binary Trees ExplainedA binary tree is nodes with up to two children. Learn the terminology, the three depth-first traversals and level-order in JavaScript, and why tree problems are almost always recursive.
  2. 2Binary Search TreesA binary search tree keeps everything smaller on the left and larger on the right, giving O(log n) search, insert and delete — until it degenerates into a list. Learn both sides in JavaScript.
  3. 3Common Tree ProblemsDepth, diameter, lowest common ancestor, path sums and symmetry — the tree questions asked most often, each solved recursively in JavaScript.