Binary Trees
Binary trees are the foundation of hierarchical data. Learn the three traversal orders and recursion.
Lessons
1. Intro
Why binary trees matter
2. Binary Tree Node
The building block
3. Tree Properties
Counting nodes and edges
4. Three Traversal Orders
Preorder, inorder, postorder
5. Recursive Traversal
The natural approach
6. Problem - Binary Tree Inorder
Classic traversal problem
7. Iterative Inorder
Simulating the call stack
8. Inorder Traversal Solution
Implement your solution
9. Level Order Traversal
BFS on trees
10. Recursive Tree Thinking
Breaking into subproblems
11. Problem - Maximum Depth
Height of the tree
12. Max Depth Solution
The recursive formula
13. Problem - Same Tree
Check structural equality
14. Same Tree Solution
Recursive comparison
15. Problem - Invert Binary Tree
Mirror the tree
16. Invert Tree Solution
Swap at every node
17. Problem - Symmetric Tree
Is it a mirror of itself?
18. Symmetric Tree Solution
Compare mirror positions
19. Path Sum Pattern
Tracking values along paths
20. Problem - Path Sum
Root-to-leaf with target
21. Path Sum Solution
Subtract as you descend
22. Building Trees from Traversals
Reconstruction problems
23. Problem - Construct from Traversals
Build tree from sequences
24. Construction Algorithm
Divide and conquer
25. Construct Tree Solution
Implement your solution
26. Lowest Common Ancestor
Finding shared ancestors
27. Serialization Pattern
Converting tree to string
28. Problem - Diameter of Binary Tree
Longest path in tree
29. Diameter Solution
Track max during DFS
30. Problem - Flatten to Linked List
Preorder to list
31. Flatten Solution
Morris-like approach
32. Morris Traversal
O(1) space traversal
33. Binary Tree from Array
Level-order construction
34. Quiz: Binary Trees
Test your understanding
35. Section Recap
What you learned
Practice Problems
Tree DP problem maximizing sum of edge weights. Teaches greedy decisions at each node based on subtree structure.
Find tree diameter using two BFS/DFS. Fundamental technique used in many tree optimization problems.
Count pairs at distance k using centroid decomposition or DP. Classic tree distance query problem.
Check if vertices lie on a root-to-leaf path. Teaches DFS ordering and LCA concepts.
DFS with constraint tracking. Count valid root-to-leaf paths avoiding consecutive marked nodes.
LCA with binary lifting for distance queries. Essential technique for tree path problems.
Tree DP counting valid edge removals. Teaches state tracking for subtree partitioning.
Euler tour + segment tree for subtree updates. Flattening tree to array for range queries.
Greedy selection on tree with depth and subtree size. Maximize happiness by choosing k industrial cities.
Euler tour with bitmask segment tree. Count distinct colors in subtrees efficiently.
Path counting with LCA and difference arrays. Count edge usage across multiple paths.
Tree operations with segment tree on Euler tour. Fill subtree, empty vertex, query operations.
Master iterative inorder with explicit stack. Foundation for Morris traversal understanding.
BFS on trees using queue. Essential pattern for level-by-level processing.
Simple recursion demonstrating tree height calculation. Foundation for tree DP.
Classic tree construction using traversal properties. Teaches index mapping optimization.
Fundamental LCA algorithm. Returns first node where both targets found in different subtrees.
Classic tree DP computing maximum path through each node. Global vs return value distinction.
Diameter as max(left_height + right_height) at any node. Simple but important tree property.
Design problem for tree encoding. Multiple approaches: preorder with nulls, level-order, etc.