Binary Trees

Binary trees are the foundation of hierarchical data. Learn the three traversal orders and recursion.

35 lessons
172 min

Lessons

1. Intro

Why binary trees matter

2m

2. Binary Tree Node

The building block

3m

3. Tree Properties

Counting nodes and edges

2m

4. Three Traversal Orders

Preorder, inorder, postorder

3m

5. Recursive Traversal

The natural approach

3m

6. Problem - Binary Tree Inorder

Classic traversal problem

2m1 problems

7. Iterative Inorder

Simulating the call stack

4m

8. Inorder Traversal Solution

Implement your solution

10m1 problems

9. Level Order Traversal

BFS on trees

3m

10. Recursive Tree Thinking

Breaking into subproblems

3m

11. Problem - Maximum Depth

Height of the tree

2m1 problems

12. Max Depth Solution

The recursive formula

8m1 problems

13. Problem - Same Tree

Check structural equality

2m1 problems

14. Same Tree Solution

Recursive comparison

8m1 problems

15. Problem - Invert Binary Tree

Mirror the tree

2m1 problems

16. Invert Tree Solution

Swap at every node

8m1 problems

17. Problem - Symmetric Tree

Is it a mirror of itself?

2m1 problems

18. Symmetric Tree Solution

Compare mirror positions

10m1 problems

19. Path Sum Pattern

Tracking values along paths

3m

20. Problem - Path Sum

Root-to-leaf with target

2m1 problems

21. Path Sum Solution

Subtract as you descend

8m1 problems

22. Building Trees from Traversals

Reconstruction problems

4m

23. Problem - Construct from Traversals

Build tree from sequences

3m1 problems

24. Construction Algorithm

Divide and conquer

4m

25. Construct Tree Solution

Implement your solution

15m1 problems

26. Lowest Common Ancestor

Finding shared ancestors

4m

27. Serialization Pattern

Converting tree to string

4m

28. Problem - Diameter of Binary Tree

Longest path in tree

3m1 problems

29. Diameter Solution

Track max during DFS

12m1 problems

30. Problem - Flatten to Linked List

Preorder to list

3m1 problems

31. Flatten Solution

Morris-like approach

15m1 problems

32. Morris Traversal

O(1) space traversal

4m

33. Binary Tree from Array

Level-order construction

3m

34. Quiz: Binary Trees

Test your understanding

5m1 problems

35. Section Recap

What you learned

3m

Practice Problems

1.
Parsa's Humongous TreeCodeforcesmedium

Tree DP problem maximizing sum of edge weights. Teaches greedy decisions at each node based on subtree structure.

2.
Tree DiameterCodeforcesmedium

Find tree diameter using two BFS/DFS. Fundamental technique used in many tree optimization problems.

3.
Distance in TreeCodeforcesmedium

Count pairs at distance k using centroid decomposition or DP. Classic tree distance query problem.

4.
Tree QueriesCodeforcesmedium

Check if vertices lie on a root-to-leaf path. Teaches DFS ordering and LCA concepts.

5.
Kefa and ParkCodeforcesmedium

DFS with constraint tracking. Count valid root-to-leaf paths avoiding consecutive marked nodes.

6.

LCA with binary lifting for distance queries. Essential technique for tree path problems.

7.
Appleman and TreeCodeforcesmedium

Tree DP counting valid edge removals. Teaches state tracking for subtree partitioning.

8.
Subtree QueriesCodeforcesmedium

Euler tour + segment tree for subtree updates. Flattening tree to array for range queries.

9.
Linova and KingdomCodeforcesmedium

Greedy selection on tree with depth and subtree size. Maximize happiness by choosing k industrial cities.

10.
New Year TreeCodeforcesmedium

Euler tour with bitmask segment tree. Count distinct colors in subtrees efficiently.

11.
Fools and RoadsCodeforcesmedium

Path counting with LCA and difference arrays. Count edge usage across multiple paths.

12.
Water TreeCodeforceshard

Tree operations with segment tree on Euler tour. Fill subtree, empty vertex, query operations.

13.

Master iterative inorder with explicit stack. Foundation for Morris traversal understanding.

14.

BFS on trees using queue. Essential pattern for level-by-level processing.

15.

Simple recursion demonstrating tree height calculation. Foundation for tree DP.

16.

Classic tree construction using traversal properties. Teaches index mapping optimization.

17.

Fundamental LCA algorithm. Returns first node where both targets found in different subtrees.

18.

Classic tree DP computing maximum path through each node. Global vs return value distinction.

19.

Diameter as max(left_height + right_height) at any node. Simple but important tree property.

20.

Design problem for tree encoding. Multiple approaches: preorder with nulls, level-order, etc.

Ready to start learning?

Access all 35 lessons with interactive content and progress tracking.