WORLD 3
TREES
Solve a smaller copy: ask each subtree, read level by level, pass down limits, walk a tree of letters.
STAGE 0 Trees tutorial
0:00 / 0:00
The move
Trust each child to answer the same question, then combine their answers at the node. A missing child is the base case. Three lines (base case, recurse left and right, combine) solve most tree problems; a queue reads the tree level by level.
- Spot it
- The problem says binary tree, root, subtree or BST, or asks for a depth, a height or a path.
- Cost
- Each node is visited once: O(n) time, and O(h) space for the recursion stack, where h is the height.
16 STAGES Trees problems
☆☆☆☆☆ Clear 5 to finish this world.
EASY1:28Invert Binary Tree
Tree recursionLC 226▶ START
EASY1:29Maximum Depth of Binary Tree
Tree recursionLC 104▶ START
EASY1:42Same Tree
Tree recursionLC 100▶ START
EASY1:40Diameter of Binary Tree
Tree recursionLC 543▶ START
EASY1:39Balanced Binary Tree
Tree recursionLC 110▶ START
EASY1:39Subtree of Another Tree
Tree recursionLC 572▶ START
MEDIUM1:30Level Order Traversal
Breadth-first searchLC 102▶ START
MEDIUM1:33Binary Tree Right Side View
Breadth-first searchLC 199▶ START
MEDIUM1:38Validate BST
Tree recursionLC 98▶ START
EASY1:37Convert Sorted Array to Binary Search Tree
Tree recursionLC 108▶ START
MEDIUM1:38Kth Smallest Element in a BST
Binary search treeLC 230▶ START
MEDIUM1:34Lowest Common Ancestor
Tree recursionLC 236▶ START
MEDIUM1:47Construct Binary Tree from Preorder and Inorder Traversal
RecursionLC 105▶ START
MEDIUM1:33Implement Trie
TrieLC 208▶ START
HARD1:43Binary Tree Maximum Path Sum
Tree recursionLC 124▶ START
HARD1:43Serialize and Deserialize Binary Tree
Tree traversalLC 297▶ START