Trees & BST
This is a premium interview-preparation course on the data structure that shows up in more interviews than any other: the **binary tree**, and its ordered cousin the **binary search tree**. Trees reward a single mental shift — stop thinking about the whole structure and start thinking about one node, its left subtree, and its right subtree. Almost every problem here is solved by asking what a node needs from its children (a bottom-up **depth-first** pass) or what it needs to pass down (a top-down pass), or by sweeping the tree one level at a time (**breadth-first**). You will start with the vocabulary — nodes, height, depth, balance, and the four traversal orders — then work through curated problems grouped by the patterns interviewers test: DFS traversals, BFS level-order, the BST ordering invariant, recursive subtree aggregation, rebuilding a tree from its traversals, and advanced problems that combine these ideas. Every lesson visualises the recursion or the level sweep on a concrete tree before showing a clean Java 17 solution.
30
Curated lessons
7
Learning modules
9h 3m
Est. study time
12
Target companies
What you'll learn
- Choose DFS (preorder, inorder, postorder) or BFS level-order based on what each node needs.
- Turn a recursive definition into code: base case, recurse on children, combine results.
- Exploit the BST ordering invariant to search, insert, delete, and validate in O(h).
- Rebuild trees from traversal orders and serialize them for transport.
Your progress
0 / 30
problems completed
30 available now · keep going!
Mark lessons complete as you work through them. Progress is saved on this device and syncs to your account when you sign in.
Learning roadmap
Work top to bottom — each module builds on the last.
- 1
Tree Fundamentals
One node, a left subtree, and a right subtreeThe vocabulary every tree problem assumes: binary trees, the BST ordering invariant, height vs depth, balance, and the four traversal orders.
6/6 problems46m - 2
DFS Traversals
Recurse left, visit, recurse right (reorder as needed)Depth-first order in code: inorder, preorder, and postorder, plus the postorder combine step that computes a subtree's height.
4/4 problems1h 3m - 3
BFS Pattern
Queue holds one level; process it, enqueue the nextSweep the tree one level at a time with a queue: level order, zigzag, the rightmost node per level, and per-level aggregates.
4/4 problems1h 3m - 4
BST Pattern
Compare with the node, then go left or rightUse the left-smaller / right-larger invariant to validate, search, insert, delete, and find a lowest common ancestor in O(h).
5/5 problems1h 43m - 5
Recursive Tree Problems
Return one value up; track a global best on the sideAggregate information from subtrees: diameter, balance, root-to-leaf path sums, and the maximum path sum that bends through a node.
4/4 problems1h 21m - 6
Tree Construction
Preorder/postorder picks the root; inorder splits the sidesRebuild a tree from its traversals and turn a tree into a string and back — the interview test of whether you truly understand traversal order.
3/3 problems1h 25m - 7
Advanced Trees
Compose DFS, BST order, and subtree DPProblems that combine the patterns: lowest common ancestor of any binary tree, order statistics on a BST, repairing a broken BST, and tree DP.
4/4 problems1h 42m
Modules
7 themed modules from fundamentals to advanced.
Tree Fundamentals
The vocabulary every tree problem assumes: binary trees, the BST ordering invariant, height vs depth, balance, and the four traversal orders.
DFS Traversals
Depth-first order in code: inorder, preorder, and postorder, plus the postorder combine step that computes a subtree's height.
BFS Pattern
Sweep the tree one level at a time with a queue: level order, zigzag, the rightmost node per level, and per-level aggregates.
BST Pattern
Use the left-smaller / right-larger invariant to validate, search, insert, delete, and find a lowest common ancestor in O(h).
Recursive Tree Problems
Aggregate information from subtrees: diameter, balance, root-to-leaf path sums, and the maximum path sum that bends through a node.
Tree Construction
Rebuild a tree from its traversals and turn a tree into a string and back — the interview test of whether you truly understand traversal order.
Advanced Trees
Problems that combine the patterns: lowest common ancestor of any binary tree, order statistics on a BST, repairing a broken BST, and tree DP.
Full curriculum
24 problems and 6 concept lessons in learning order.
- 1Binary TreeTree Fundamentals Concept
- 2Binary Search Tree (BST)Tree Fundamentals Concept
- 3HeightTree Fundamentals Concept
- 4DepthTree Fundamentals Concept
- 5Balanced TreeTree Fundamentals Concept
- 6TraversalsTree Fundamentals Concept
- 7Binary Tree Inorder TraversalDFS TraversalsEasy
- 8Binary Tree Preorder TraversalDFS TraversalsEasy
- 9Binary Tree Postorder TraversalDFS TraversalsEasy
- 10Maximum Depth of Binary TreeDFS TraversalsEasy
- 11Binary Tree Level Order TraversalBFS PatternMedium
- 12Binary Tree Zigzag Level Order TraversalBFS PatternMedium
- 13Binary Tree Right Side ViewBFS PatternMedium
- 14Average of Levels in Binary TreeBFS PatternEasy
- 15Validate Binary Search TreeBST PatternMedium
- 16Search in a Binary Search TreeBST PatternEasy
- 17Insert into a Binary Search TreeBST PatternMedium
- 18Delete Node in a BSTBST PatternMedium
- 19Lowest Common Ancestor of a Binary Search TreeBST PatternMedium
- 20Diameter of Binary TreeRecursive Tree ProblemsEasy
- 21Balanced Binary TreeRecursive Tree ProblemsEasy
- 22Path SumRecursive Tree ProblemsEasy
- 23Binary Tree Maximum Path SumRecursive Tree ProblemsHard
- 24Construct Binary Tree from Preorder and Inorder TraversalTree ConstructionMedium
- 25Construct Binary Tree from Inorder and Postorder TraversalTree ConstructionMedium
- 26Serialize and Deserialize Binary TreeTree ConstructionHard
- 27Lowest Common Ancestor of a Binary TreeAdvanced TreesMedium
- 28Kth Smallest Element in a BSTAdvanced TreesMedium
- 29Recover Binary Search TreeAdvanced TreesMedium
- 30House Robber IIIAdvanced TreesMedium