|
21 | 21 | },
|
22 | 22 | {
|
23 | 23 | "cell_type": "code",
|
24 |
| - "execution_count": 85, |
| 24 | + "execution_count": 90, |
25 | 25 | "metadata": {},
|
26 | 26 | "outputs": [
|
27 | 27 | {
|
|
88 | 88 | },
|
89 | 89 | {
|
90 | 90 | "cell_type": "code",
|
91 |
| - "execution_count": null, |
| 91 | + "execution_count": 91, |
92 | 92 | "metadata": {},
|
93 |
| - "outputs": [], |
| 93 | + "outputs": [ |
| 94 | + { |
| 95 | + "ename": "IndentationError", |
| 96 | + "evalue": "expected an indented block (2850872121.py, line 8)", |
| 97 | + "output_type": "error", |
| 98 | + "traceback": [ |
| 99 | + "\u001b[0;36m Cell \u001b[0;32mIn[91], line 8\u001b[0;36m\u001b[0m\n\u001b[0;31m # TODO\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" |
| 100 | + ] |
| 101 | + } |
| 102 | + ], |
94 | 103 | "source": [
|
95 | 104 | "# Definition for a binary tree node.\n",
|
96 | 105 | "# class TreeNode(object):\n",
|
|
138 | 147 | },
|
139 | 148 | {
|
140 | 149 | "cell_type": "code",
|
141 |
| - "execution_count": 23, |
| 150 | + "execution_count": 106, |
142 | 151 | "metadata": {},
|
143 | 152 | "outputs": [],
|
144 | 153 | "source": [
|
|
215 | 224 | }
|
216 | 225 | },
|
217 | 226 | "source": [
|
218 |
| - "I have a binary tree, I want to find all possible way I can get from the top (or the root node) to the bottom (leaf).\n", |
219 |
| - "Since, there are multiple leaves. I have multiple paths." |
| 227 | + "Given a binary tree, the task is to find all the possible paths from the top (root node) to the botton (left) of the tree." |
220 | 228 | ]
|
221 | 229 | },
|
222 | 230 | {
|
|
240 | 248 | "[('1', '2', '4'), ('1', '2', '5'), ('1', '3', '6'), ('1', '3', '7')]\n",
|
241 | 249 | "\n",
|
242 | 250 | "# Tree # 2\n",
|
| 251 | + "# the data type in different nodes can be different too\n", |
243 | 252 | "[1, 2, 3, 3, 5 , 'my_Leaf']\n",
|
244 | 253 | "#hould return\n",
|
245 | 254 | "[('1', '2', '3'), ('1', '2', '5'), ('1', '3', 'my_Leaf')]"
|
|
255 | 264 | },
|
256 | 265 | {
|
257 | 266 | "cell_type": "code",
|
258 |
| - "execution_count": 86, |
| 267 | + "execution_count": 93, |
259 | 268 | "metadata": {},
|
260 | 269 | "outputs": [],
|
261 | 270 | "source": [
|
|
294 | 303 | },
|
295 | 304 | {
|
296 | 305 | "cell_type": "code",
|
297 |
| - "execution_count": 87, |
| 306 | + "execution_count": 95, |
298 | 307 | "metadata": {},
|
299 | 308 | "outputs": [
|
300 | 309 | {
|
|
355 | 364 | "source": [
|
356 | 365 | "# Your answer here\n",
|
357 | 366 | "\n",
|
358 |
| - "It can be broken down into a recursive method, because a big tree is made up a smaller trees. A binary free is made up of 2 smaller trees" |
| 367 | + "It can be broken down into a recursive method, because a big tree is made up a smaller trees. A binary free is made up of 2 smaller trees.\n", |
| 368 | + "\n", |
| 369 | + "\n", |
| 370 | + "1. Start at the root → Add the current node’s value to the path list and move down the tree.\n", |
| 371 | + "2. Recursively explore left and right children → Call DFS on the left child first, then the right child. \n", |
| 372 | + " 2.1 For each node, I store the path taken into a list - 'path'\n", |
| 373 | + "3. If a leaf node is reached (no left or right child) → Convert path to a tuple (so that it is immutable now, and can-not be changed) and append it in the list - result\n", |
| 374 | + "4. Backtrack → Remove the last node from path (pop()) and try a different path.\n", |
| 375 | + "5. Continue until all nodes are visited (if not node, base case) → Once all paths are collected, return the final result list." |
359 | 376 | ]
|
360 | 377 | },
|
361 | 378 | {
|
|
400 | 417 | "source": [
|
401 | 418 | "# Your answer here\n",
|
402 | 419 | "\n",
|
403 |
| - "Another method is using a stack data-structure. First understand how this DS works:\n", |
| 420 | + "Instead of using a recursive method, I can also use stack data structures\n", |
404 | 421 | "\n",
|
405 |
| - "A stack is a data structure that works like a stack of plates:\n", |
| 422 | + "First explaination of how a stack data strucutre would work. A stack is a data structure that works like a stack of plates:\n", |
406 | 423 | "\n",
|
407 |
| - "1. You add items to the top (push).\n", |
408 |
| - "2. You remove items from the top (pop). - LIFO\n", |
| 424 | + "1. You add items to the top (push). (Implement by adding a new element to the left hand side of the list, do-not use append. Use insert(0 , 'value'))\n", |
| 425 | + "2. You remove items from the top (pop). - LIFO. (Use pop to remove the right most element)\n", |
409 | 426 | "3. The last item added is the first one removed (Last In, First Out → LIFO)\n",
|
410 | 427 | "\n",
|
411 |
| - "This can be implemented in python using lists\n", |
412 |
| - "\n", |
413 |
| - "Now depth first seach using stacks:\n", |
414 |
| - "\n", |
| 428 | + "This is how we can implement DFS using stacks\n", |
415 | 429 | "\n",
|
416 |
| - "1. Start with the root and put it in the stack.\n", |
| 430 | + "1. Start with the root and put it in the stack. (This the root node on the top of the tree)\n", |
417 | 431 | "2. Take a node from the stack, check if it’s a leaf (has no children).\n",
|
418 | 432 | " - If it’s a leaf, save its path.\n",
|
419 |
| - " - If not, add its children to the stack (right child first, then left).\n", |
| 433 | + " - If not, add its children to the stack (right child first, then left). -> This way the childs are also added to the 'list' of things to check\n", |
420 | 434 | "3. Repeat until we’ve visited all nodes.\n",
|
421 | 435 | "4. Since we use a stack, we process the left side first (because stacks work in Last In, First Out order)."
|
422 | 436 | ]
|
|
0 commit comments