Stack [mp]
Micro pattern¶
Description¶
Use last-in, first-out storage for nested syntax, deferred values, traversal frames, or reversed processing.
Problems¶
Delimiters and expressions¶
Push unfinished syntax or operands and resolve them when an operator appears.
- 20. Valid Parentheses (Top Easy)
- 150. Evaluate Reverse Polish Notation (Top Medium)
Augmented stack¶
Store each value with metadata valid at its stack depth.
- 155. Min Stack (Top Easy)
- 84. Largest Rectangle in Histogram (Top Hard)
Simulate recursion or traversal¶
Push nodes or states together with context needed when popped.
- DFS (DSA)
- Inorder traversal (DSA)
- Postorder traversal (DSA)
- Preorder traversal (DSA)
- 98. Validate Binary Search Tree (Top Easy)
- 104. Maximum Depth of Binary Tree (Top Easy)
- 404. Sum of Left Leaves (Google Prep)
- 94. Binary Tree Inorder Traversal (Top Medium)
- 230. Kth Smallest Element in a BST (Top Medium)
- 130. Surrounded Regions (Top Hard)
- 236. Lowest Common Ancestor of a Binary Tree (Top Hard)
- 124. Binary Tree Maximum Path Sum (Top Hard)
- 841. Keys and Rooms (Graph Theory)
- 1376. Time Needed to Inform All Employees (Graph Theory)
- 1466. Reorder Routes to Make All Paths Lead to the City Zero (Graph Theory)
- 1192. Critical Connections in a Network (Graph Theory)
- 934. Shortest Bridge (Graph Theory)
- 1306. Jump Game III (Graph Theory)
- 1020. Number of Enclaves (Graph Theory)
- 1254. Number of Closed Islands (Graph Theory)
- 695. Max Area of Island (Graph Theory)
- 417. Pacific Atlantic Water Flow (Graph Theory)
- 785. Is Graph Bipartite? (Graph Theory)
- 721. Accounts Merge (Graph Theory)
- 337. House Robber III (Dynamic Programming)
Reverse input order¶
Push forward-order digits and pop them in arithmetic order.
- 445. Add Two Numbers II (Google Prep)
Nested iterator state¶
Keep an iterator for each active nesting level.
- 341. Flatten Nested List Iterator (Top Hard)
Monotonic stack¶
Keep unresolved candidates ordered until the current value closes their range.
- 84. Largest Rectangle in Histogram (Top Hard)