~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Binary trees
Recursive return values (height, best path), BFS by level, BST invariants.
Main path: 8 problems, about 2 hours.
Notes
Recognise it when: you're given a TreeNode. Most answers are a post-order recursion that returns something to the parent.
def height(node): # diameter = max over nodes of left + right
nonlocal best
if not node: return 0
l, r = height(node.left), height(node.right)
best = max(best, l + r)
return 1 + max(l, r)
- Level order: BFS processing
len(queue)nodes per level. - Validate a BST: pass
(lo, hi)bounds down. Checking only the immediate children is the classic bug. - LCA: if both sides return non-None, this node is the LCA. Otherwise return whichever side isn't None.
- Serialize: preorder with null markers, and deserialize with an iterator.
Gotchas: deep skewed trees hit the recursion limit, so go iterative or raise it. Tell apart "return value" and "global best".
31 problems
Interview roadmap
Trees Traversals, BSTs, and answers built bottom-up.
Binary trees guide
- Play first: The Door Maze 1-minute game Binary search tree lookup
- Preorder, inorder and postorder basics py · c++ · java easy
- Orchard: branches that need a prop py · c++ · java easy
- Search in a Binary Search Tree code the boss py · c++ · java easy
- Maximum Depth of Binary Tree ▶ boss game py · c++ · java easy
- Invert Binary Tree ▶ boss game py · c++ · java easy
- Binary Tree Level Order Traversal ▶ boss game py · c++ · java medium
- Validate Binary Search Tree py · c++ · java medium
- Serialize and Deserialize Binary Tree py · c++ · java hard
More practice: 23 problems
- Same Tree py · c++ · java easy
- Diameter of Binary Tree py · c++ · java easy
- Balanced Binary Tree py · c++ · java easy
- Subtree of Another Tree py · c++ · java easy
- Binary Tree Zigzag Level Order Traversal py · c++ · java medium
- Insert into a Binary Search Tree py · c++ · java medium
- Cheapest root-to-leaf path CitadelMeta py · c++ · java medium
- Path Sum II py · c++ · java medium
- Lowest Common Ancestor of a Binary Search Tree ▶ boss game py · c++ · java medium
- Binary Tree Right Side View py · c++ · java medium
- Binary Tree Vertical Order Traversal py · c++ · java medium
- Count Good Nodes in Binary Tree py · c++ · java medium
- Kth Smallest Element in a BST py · c++ · java medium
- Binary Search Tree Iterator py · c++ · java medium
- Lowest Common Ancestor of a Binary Tree py · c++ · java medium
- All Nodes Distance K in Binary Tree py · c++ · java medium
- Path Sum III py · c++ · java medium
- Delete Node in a BST py · c++ · java medium
- Binary search tree from scratch Citadel py · c++ · java medium
- Directions inside a Fibonacci tree Databricks py · c++ · java medium
- Construct Binary Tree from Preorder and Inorder Traversal py · c++ · java medium
- N-ary tree sum and leaf next-links assessment 3 levels Citadel py · c++ · java hard
- Tree height after deleting nodes assessment 3 levels Snowflake py · c++ · java hard