~/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.
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".
24 problems
Interview roadmap
Trees Traversals, BSTs, and answers built bottom-up.
Binary trees guide
- Basics: preorder, inorder and postorder basics easy
- Orchard: branches that need a prop py · c++ · java easy
- Diameter of Binary Tree py · c++ · java easy
- Binary Tree Level Order Traversal py · c++ · java easy
- Validate Binary Search Tree py · c++ · java medium
- Lowest Common Ancestor of a Binary Tree medium
- Serialize and Deserialize Binary Tree medium
- Binary search tree from scratch Citadel py · c++ · java medium
- N-ary tree sum and leaf next-links 3 levels Citadel hard
- OA: Tree height after deleting nodes 3 levels Snowflake hard
- Maximum Depth of Binary Tree py · c++ · java easy
- Cheapest root-to-leaf path CitadelMeta py · c++ · java medium
- Directions inside a Fibonacci tree Databricks py · c++ · java medium
- Invert Binary Tree py · c++ · java easy
- Balanced Binary Tree py · c++ · java easy
- Same Tree py · c++ · java easy
- Subtree of Another Tree py · c++ · java easy
- Lowest Common Ancestor of a Binary Search Tree py · c++ · java medium
- Binary Tree Right Side View py · c++ · java medium
- Count Good Nodes in Binary Tree py · c++ · java medium
- Kth Smallest Element in a BST py · c++ · java medium
- Construct Binary Tree from Preorder and Inorder Traversal py · c++ · java medium
- Insert into a Binary Search Tree py · c++ · java easy
- Delete Node in a BST py · c++ · java medium