19. Binary Trees

Traversals, BFS, height/diameter, views, LCA, construction and serialization.

🎬 Episodes 53–60: video coming soon
Your progress0 of 20
8easy
9medium
3hard

Understand the topic

A binary tree is nodes with up to two children. Almost every tree solution is recursion: solve for the left subtree, solve for the right subtree, combine at the node. Ask yourself what each call should return (a height, a sum, a node, a pair of values).

Traversals decide when you visit the node. Preorder is node, left, right. Inorder is left, node, right. Postorder is left, right, node. Level order (BFS with a queue) goes row by row and is used for views and widths.

#ProblemPatternLevel
1 Binary Tree Inorder Traversal #94 Iterative inorder with a stack Easy LeetCode ↗
2 Binary Tree Preorder Traversal #144 Iterative preorder with a stack Easy LeetCode ↗
3 Binary Tree Postorder Traversal #145 Reverse of modified preorder Easy LeetCode ↗
4 Maximum Depth of Binary Tree #104 Recursive height Easy LeetCode ↗
5 Symmetric Tree #101 Mirror recursion Easy LeetCode ↗
6 Invert Binary Tree #226 Swap children recursively Easy LeetCode ↗
7 Diameter of Binary Tree #543 Height with a global best Easy LeetCode ↗
8 Balanced Binary Tree #110 Height with early exit Easy LeetCode ↗
9 Binary Tree Level Order Traversal #102 BFS by levels Medium LeetCode ↗
10 Binary Tree Zigzag Level Order Traversal #103 BFS with alternating direction Medium LeetCode ↗
11 Binary Tree Right Side View #199 Rightmost per level Medium LeetCode ↗
12 Lowest Common Ancestor of a Binary Tree #236 Postorder search Medium LeetCode ↗
13 Construct Binary Tree from Preorder and Inorder Traversal #105 Divide by root position Medium LeetCode ↗
14 Construct Binary Tree from Inorder and Postorder Traversal #106 Divide by root position Medium LeetCode ↗
15 Maximum Width of Binary Tree #662 BFS with position indexes Medium LeetCode ↗
16 Flatten Binary Tree to Linked List #114 Morris-style rewiring Medium LeetCode ↗
17 Count Good Nodes in Binary Tree #1448 DFS carrying the path maximum Medium LeetCode ↗
18 Binary Tree Maximum Path Sum #124 Gain function with global best Hard LeetCode ↗
19 Serialize and Deserialize Binary Tree #297 Preorder with null markers Hard LeetCode ↗
20 Vertical Order Traversal of a Binary Tree #987 Coordinates + sort Hard LeetCode ↗

Work top to bottom — each list goes from easy to hard. Try a problem for at least 15 minutes before opening a hint. Ticks are saved in your browser.