|
| 1 | +# LeetCode Helper Functions |
| 2 | + |
| 3 | +This folder contains utility functions and data structures to help with debugging and solving LeetCode problems. |
| 4 | + |
| 5 | +## 📚 Contents |
| 6 | + |
| 7 | +- **[data_structures.py](data_structures.py)**: Common data structures (TreeNode, ListNode) |
| 8 | +- **[tree_utils.py](tree_utils.py)**: Binary tree helper functions |
| 9 | +- **[linked_list_utils.py](linked_list_utils.py)**: Linked list helper functions |
| 10 | +- **[algorithms.py](algorithms.py)**: Common algorithms (BFS, DFS, tree search) |
| 11 | + |
| 12 | +## 🚀 Demo Notebooks |
| 13 | + |
| 14 | +Interactive Jupyter notebooks demonstrating the helper functions: |
| 15 | + |
| 16 | +- **[Binary Trees Demo](demo_binary_trees.ipynb)** [](https://colab.research.google.com/github/ContextLab/leetcode-solutions/blob/main/helpers/demo_binary_trees.ipynb) |
| 17 | +- **[Linked Lists Demo](demo_linked_lists.ipynb)** [](https://colab.research.google.com/github/ContextLab/leetcode-solutions/blob/main/helpers/demo_linked_lists.ipynb) |
| 18 | + |
| 19 | +## 📖 Quick Start |
| 20 | + |
| 21 | +### Installation |
| 22 | + |
| 23 | +From the root of the repository: |
| 24 | + |
| 25 | +```python |
| 26 | +from helpers import * |
| 27 | +``` |
| 28 | + |
| 29 | +### Binary Trees |
| 30 | + |
| 31 | +#### Creating Trees |
| 32 | + |
| 33 | +```python |
| 34 | +from helpers import list_to_tree, print_tree |
| 35 | + |
| 36 | +# Create a tree from a list (LeetCode format) |
| 37 | +tree = list_to_tree([1, 2, 3, 4, 5, None, 7]) |
| 38 | + |
| 39 | +# Visualize it |
| 40 | +print_tree(tree) |
| 41 | +# Output: |
| 42 | +# Root: 1 |
| 43 | +# ├─ L: 2 |
| 44 | +# │ ├─ L: 4 |
| 45 | +# │ └─ R: 5 |
| 46 | +# └─ R: 3 |
| 47 | +# └─ R: 7 |
| 48 | +``` |
| 49 | + |
| 50 | +#### Converting Trees |
| 51 | + |
| 52 | +```python |
| 53 | +from helpers import tree_to_list |
| 54 | + |
| 55 | +# Convert a tree back to a list |
| 56 | +tree_list = tree_to_list(tree) |
| 57 | +print(tree_list) # [1, 2, 3, 4, 5, None, 7] |
| 58 | +``` |
| 59 | + |
| 60 | +#### Tree Traversals |
| 61 | + |
| 62 | +```python |
| 63 | +from helpers import bfs_traversal, dfs_preorder, dfs_inorder, dfs_postorder |
| 64 | + |
| 65 | +tree = list_to_tree([1, 2, 3, 4, 5]) |
| 66 | + |
| 67 | +print(bfs_traversal(tree)) # [1, 2, 3, 4, 5] |
| 68 | +print(dfs_preorder(tree)) # [1, 2, 4, 5, 3] |
| 69 | +print(dfs_inorder(tree)) # [4, 2, 5, 1, 3] |
| 70 | +print(dfs_postorder(tree)) # [4, 5, 2, 3, 1] |
| 71 | +``` |
| 72 | + |
| 73 | +### Linked Lists |
| 74 | + |
| 75 | +#### Creating Linked Lists |
| 76 | + |
| 77 | +```python |
| 78 | +from helpers import list_to_linked_list, print_linked_list |
| 79 | + |
| 80 | +# Create a linked list from a Python list |
| 81 | +head = list_to_linked_list([1, 2, 3, 4, 5]) |
| 82 | + |
| 83 | +# Visualize it |
| 84 | +print_linked_list(head) |
| 85 | +# Output: 1 -> 2 -> 3 -> 4 -> 5 -> None |
| 86 | +``` |
| 87 | + |
| 88 | +#### Converting Linked Lists |
| 89 | + |
| 90 | +```python |
| 91 | +from helpers import linked_list_to_list |
| 92 | + |
| 93 | +# Convert back to a Python list |
| 94 | +result = linked_list_to_list(head) |
| 95 | +print(result) # [1, 2, 3, 4, 5] |
| 96 | +``` |
| 97 | + |
| 98 | +#### Accessing Nodes |
| 99 | + |
| 100 | +```python |
| 101 | +from helpers import get_node_at_index, get_linked_list_length |
| 102 | + |
| 103 | +head = list_to_linked_list([10, 20, 30, 40, 50]) |
| 104 | + |
| 105 | +# Get length |
| 106 | +length = get_linked_list_length(head) # 5 |
| 107 | + |
| 108 | +# Get node at index |
| 109 | +node = get_node_at_index(head, 2) |
| 110 | +print(node.val) # 30 |
| 111 | +``` |
| 112 | + |
| 113 | +## 📋 Complete Function Reference |
| 114 | + |
| 115 | +### Data Structures |
| 116 | + |
| 117 | +#### TreeNode |
| 118 | +```python |
| 119 | +class TreeNode: |
| 120 | + def __init__(self, val=0, left=None, right=None) |
| 121 | +``` |
| 122 | +Standard binary tree node used in LeetCode problems. |
| 123 | + |
| 124 | +#### ListNode |
| 125 | +```python |
| 126 | +class ListNode: |
| 127 | + def __init__(self, val=0, next=None) |
| 128 | +``` |
| 129 | +Standard linked list node used in LeetCode problems. |
| 130 | + |
| 131 | +### Binary Tree Functions |
| 132 | + |
| 133 | +#### Conversion Functions |
| 134 | +- `list_to_tree(values)`: Convert a list to a binary tree (level-order) |
| 135 | +- `tree_to_list(root)`: Convert a binary tree to a list (level-order) |
| 136 | + |
| 137 | +#### Visualization Functions |
| 138 | +- `print_tree(root)`: Print a visual representation of a tree |
| 139 | +- `visualize_tree(root)`: Get a string representation of a tree |
| 140 | + |
| 141 | +#### Tree Properties |
| 142 | +- `get_tree_height(root)`: Get the height of a tree |
| 143 | +- `count_nodes(root)`: Count total nodes in a tree |
| 144 | + |
| 145 | +### Linked List Functions |
| 146 | + |
| 147 | +#### Conversion Functions |
| 148 | +- `list_to_linked_list(values)`: Convert a Python list to a linked list |
| 149 | +- `linked_list_to_list(head)`: Convert a linked list to a Python list |
| 150 | + |
| 151 | +#### Visualization Functions |
| 152 | +- `print_linked_list(head)`: Print a visual representation of a linked list |
| 153 | + |
| 154 | +#### List Properties |
| 155 | +- `get_linked_list_length(head)`: Get the length of a linked list |
| 156 | +- `get_node_at_index(head, index)`: Get the node at a specific index |
| 157 | + |
| 158 | +### Algorithm Functions |
| 159 | + |
| 160 | +#### Tree Traversals |
| 161 | +- `bfs_traversal(root)`: Breadth-first search (level-order) traversal |
| 162 | +- `dfs_preorder(root)`: Depth-first search pre-order traversal |
| 163 | +- `dfs_inorder(root)`: Depth-first search in-order traversal |
| 164 | +- `dfs_postorder(root)`: Depth-first search post-order traversal |
| 165 | +- `level_order_traversal(root)`: Level-order traversal grouped by level |
| 166 | + |
| 167 | +#### Tree Search |
| 168 | +- `find_path_to_node(root, target)`: Find path from root to a target node |
| 169 | +- `lowest_common_ancestor(root, p, q)`: Find the lowest common ancestor of two nodes |
| 170 | +- `search_bst(root, target)`: Search for a value in a Binary Search Tree |
| 171 | +- `is_valid_bst(root)`: Check if a tree is a valid Binary Search Tree |
| 172 | + |
| 173 | +## 💡 Usage Examples |
| 174 | + |
| 175 | +### Example 1: Testing Your Solution |
| 176 | + |
| 177 | +```python |
| 178 | +from helpers import list_to_tree, tree_to_list |
| 179 | + |
| 180 | +def your_solution(root): |
| 181 | + # Your solution code here |
| 182 | + pass |
| 183 | + |
| 184 | +# Test with LeetCode test case |
| 185 | +test_input = [1, 2, 3, 4, 5, None, 7] |
| 186 | +tree = list_to_tree(test_input) |
| 187 | +result = your_solution(tree) |
| 188 | +print(tree_to_list(result)) |
| 189 | +``` |
| 190 | + |
| 191 | +### Example 2: Debugging Tree Structure |
| 192 | + |
| 193 | +```python |
| 194 | +from helpers import list_to_tree, print_tree, get_tree_height, count_nodes |
| 195 | + |
| 196 | +tree = list_to_tree([1, 2, 3, 4, 5, 6, 7, 8]) |
| 197 | + |
| 198 | +print("Tree structure:") |
| 199 | +print_tree(tree) |
| 200 | + |
| 201 | +print(f"\nHeight: {get_tree_height(tree)}") |
| 202 | +print(f"Total nodes: {count_nodes(tree)}") |
| 203 | +``` |
| 204 | + |
| 205 | +### Example 3: Visualizing Linked List Operations |
| 206 | + |
| 207 | +```python |
| 208 | +from helpers import list_to_linked_list, print_linked_list |
| 209 | + |
| 210 | +# Before operation |
| 211 | +head = list_to_linked_list([1, 2, 3, 4, 5]) |
| 212 | +print("Before:") |
| 213 | +print_linked_list(head) |
| 214 | + |
| 215 | +# Your operation here (e.g., reverse) |
| 216 | +# ... |
| 217 | + |
| 218 | +print("\nAfter:") |
| 219 | +print_linked_list(head) |
| 220 | +``` |
| 221 | + |
| 222 | +### Example 4: Understanding Tree Traversals |
| 223 | + |
| 224 | +```python |
| 225 | +from helpers import list_to_tree, print_tree |
| 226 | +from helpers import bfs_traversal, dfs_preorder, dfs_inorder, dfs_postorder |
| 227 | + |
| 228 | +tree = list_to_tree([1, 2, 3, 4, 5, 6, 7]) |
| 229 | + |
| 230 | +print("Tree:") |
| 231 | +print_tree(tree) |
| 232 | +print() |
| 233 | + |
| 234 | +print("Traversals:") |
| 235 | +print(f"BFS: {bfs_traversal(tree)}") |
| 236 | +print(f"Pre-order: {dfs_preorder(tree)}") |
| 237 | +print(f"In-order: {dfs_inorder(tree)}") |
| 238 | +print(f"Post-order: {dfs_postorder(tree)}") |
| 239 | +``` |
| 240 | + |
| 241 | +## 🤝 Contributing |
| 242 | + |
| 243 | +Feel free to add more helper functions as you encounter common patterns in LeetCode problems! |
| 244 | + |
| 245 | +To add a new helper: |
| 246 | +1. Add the function to the appropriate module (`tree_utils.py`, `linked_list_utils.py`, etc.) |
| 247 | +2. Update `__init__.py` to export the function |
| 248 | +3. Add documentation and examples |
| 249 | +4. Update this README |
| 250 | + |
| 251 | +## 📝 Notes |
| 252 | + |
| 253 | +- All functions follow LeetCode's standard conventions for data structures |
| 254 | +- `None` in tree lists represents null nodes (LeetCode format) |
| 255 | +- Functions are designed to be copy-paste friendly for LeetCode submissions |
| 256 | +- Visualization functions are great for debugging but won't work on LeetCode (they don't affect solutions) |
| 257 | + |
| 258 | +## 🔗 Related Resources |
| 259 | + |
| 260 | +- [LeetCode Official Site](https://leetcode.com) |
| 261 | +- [Main Repository README](../README.md) |
| 262 | +- [Problems We've Solved](../problems) |
| 263 | + |
| 264 | +Happy coding! 🎉 |
0 commit comments