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.
20 STAGES
☆☆☆☆☆ Clear any 5 stages to beat this world, or all 20 for 100%.
EASY2:02Invert Binary Tree
Tree recursionLC 226▶ START
EASY2:04Maximum Depth of Binary Tree
Tree recursionLC 104▶ START
EASY2:07Same Tree
Tree recursionLC 100▶ START
EASY1:55Symmetric Tree
Tree recursionLC 101▶ START
EASY2:08Diameter of Binary Tree
Tree recursionLC 543▶ START
EASY2:03Balanced Binary Tree
Tree recursionLC 110▶ START
EASY2:06Subtree of Another Tree
Tree recursionLC 572▶ START
MEDIUM2:03Level Order Traversal
Breadth-first searchLC 102▶ START
MEDIUM2:02Binary Tree Right Side View
Breadth-first searchLC 199▶ START
MEDIUM2:08Validate BST
Tree recursionLC 98▶ START
MEDIUM2:03Count Good Nodes in Binary Tree
Tree recursionLC 1448▶ START
EASY2:04Convert Sorted Array to Binary Search Tree
Tree recursionLC 108▶ START
MEDIUM2:02Kth Smallest Element in a BST
Binary search treeLC 230▶ START
MEDIUM2:07Lowest Common Ancestor
Tree recursionLC 236▶ START
MEDIUM2:08Construct Binary Tree from Preorder and Inorder Traversal
Tree recursionLC 105▶ START
MEDIUM2:02Flatten Binary Tree to Linked List
Tree recursionLC 114▶ START
MEDIUM2:05Implement Trie
TrieLC 208▶ START
HARD2:00Binary Tree Maximum Path Sum
Tree recursionLC 124▶ START
HARD2:03Binary Tree Cameras
Tree recursionLC 968▶ START
HARD2:11Serialize and Deserialize Binary Tree
Tree traversalLC 297▶ START