Module 8 · 5 lessons · about 8 hours 55 minutes
Recursion and trees
Recursion and the call stack, binary trees and their traversals, passing answers up the tree, level-by-level search, and binary search trees.
- Lessons
- 0/5
- Exercises
- 0/40
- Code
- 0/6
- Problems
- 0/24
- Checkpoint
- Not taken
Already comfortable with this? Take the checkpoint cold. If you pass, move on and come back only for what you missed.
- 1Recursion and the call stackA recursive function answers a question by asking itself the same question about something smaller, until it reaches a case it can answer outright. Watch the call stack grow and shrink, learn to trust the smaller call, and make slow recursion fast with a memo and with halving.1 h 40 min
- 2Binary treesA binary tree is a set of nodes in which each node points at up to two children. Visit every node in preorder, inorder or postorder with a short recursive function, visit it in preorder with a stack of your own, and answer most tree questions by building each node's answer from its children's answers.1 h 45 min
- 3Passing answers up the treeAt every node of a tree, ask two questions. What do I need from my children? What do I hand to my parent? Diameter, balanced trees, path sums and lowest common ancestors are four answers to that pair.1 h 50 min
- 4Level by levelA queue hands out a tree's nodes one level at a time, top to bottom and left to right. Count the queue before you take a level's first node and you know where that level ends, which answers level order, the right side view and the nearest leaf.1 h 45 min
- 5Binary search treesA binary search tree keeps one promise at every node, smaller values on the left and larger values on the right. That promise lets search and insert walk a single path, makes an inorder walk come out sorted, and turns three classic interview questions into short walks.1 h 55 min
- Checkpoint12 questions. Pass with 80% to finish the module.
Problems for this module
The lessons teach some of these step by step (each says which). The rest are practice: try each in its target time, and solved problems come back for spaced re-solves.
- easyFibonacci number15 min
- mediumRaise to a power25 min
- easyMaximum depth of a binary tree15 min
- easyInvert a binary tree15 min
- easySame tree15 min
- easySubtree of another tree15 min
- easyInorder traversal15 min
- easyDiameter of a binary tree15 min
- easyBalanced binary tree15 min
- easyRoot-to-leaf path sum15 min
- mediumLowest common ancestor25 min
- mediumCount good nodes25 min
- hardMaximum path sum40 min
- mediumRebuild a tree from preorder and inorder25 min
- mediumLevel order traversal25 min
- mediumRight side view25 min
- mediumVertical order traversal25 min
- mediumZigzag level order25 min
- hardTree to text and back40 min
- mediumIs it a binary search tree?25 min
- mediumK-th smallest value in a BST25 min
- mediumLowest common ancestor in a BST25 min
- easySearch a BST15 min
- easyMinimum depth15 min