Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 09 · DSA · Four patterns the ladders lean on · about 22 min to read

Play it as a journeyTutor comes with a plan

Climbing a tree in powers of two

Trees
Trees · Open board

The tree on this board is the shape every question below is asked about.

A company has 100,000 people (a teaching value). Every person has one manager, except the head. Software holds this as a tree. The manager of a person is that person’s parent, and everyone on the way up to the head is an ancestor. Four questions arrive all day. Who is the 77,777th ancestor of this person? Who is the lowest manager above both A and B? What is the total salary under a manager? How far is each person from everyone else? Each plain answer is a walk through the tree.

By the end you will be able to answer the first two in about 17 steps, turn the tree into a line, find every person’s total distance, and sum a path while salaries change. You will solve two free LeetCode problems, 1483 and 834.

The history (optional)

Sleator and Tarjan published “A Data Structure for Dynamic Trees” in the Journal of Computer and System Sciences, volume 26, in 1983. Wikipedia credits it with the heavy path split used here. Harel and Tarjan published “Fast Algorithms for Finding Nearest Common Ancestors” in the SIAM Journal on Computing, volume 13, in May 1984.

Before you start. Trees, asked five ways found the lowest common ancestor by sending answers up, which reads the whole tree for each question. One array, one trick: the Fenwick tree gave you the Fenwick tree, which changes one number and sums a stretch of a row in about log n steps. The running total (prefix sums and difference arrays) gave you the prefix sum, a row of totals so far. Other ways to answer range questions gave you the sparse table, which saves answers for blocks whose length is a power of two.

Lesson 81 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Walking up is slow, so keep a table of jumps: binary lifting
  • The lowest common ancestor in 17 steps
  • Euler tour: turn the tree into a line
  • Rerooting: reuse the answer from the neighbour
  • Heavy-light decomposition: cut the tree into chains
  • Binary lifting, kth ancestor and the lowest common ancestor
  • Euler tour
  • Rerooting for the sum of distances
  • Heavy-light decomposition with a Fenwick tree on the chains
  • LeetCode 1483 and 834, solved
  • Random checks, and the cost at 100,000 people
  • Trade-offs and where this is used

Terms it explains: parent, ancestor, binary lifting, Euler tour, rerooting, heavy child, heavy-light decomposition, Common mistakes, B-tree, balanced, binary tree, BST, complete tree, heap, height h, priority queue, rotation, self-balancing BST (AVL / red-black), sift up/down, traversal, tree, trie.

Review DSA