Skip to content

Commit 74eb8a8

Browse files
committed
Assignment 1
1 parent 4a02dbb commit 74eb8a8

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

02_activities/assignments/assignment_1.ipynb

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
{
2323
"cell_type": "code",
24-
"execution_count": 85,
24+
"execution_count": 90,
2525
"metadata": {},
2626
"outputs": [
2727
{
@@ -88,9 +88,18 @@
8888
},
8989
{
9090
"cell_type": "code",
91-
"execution_count": null,
91+
"execution_count": 91,
9292
"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+
],
94103
"source": [
95104
"# Definition for a binary tree node.\n",
96105
"# class TreeNode(object):\n",
@@ -138,7 +147,7 @@
138147
},
139148
{
140149
"cell_type": "code",
141-
"execution_count": 23,
150+
"execution_count": 106,
142151
"metadata": {},
143152
"outputs": [],
144153
"source": [
@@ -215,8 +224,7 @@
215224
}
216225
},
217226
"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."
220228
]
221229
},
222230
{
@@ -240,6 +248,7 @@
240248
"[('1', '2', '4'), ('1', '2', '5'), ('1', '3', '6'), ('1', '3', '7')]\n",
241249
"\n",
242250
"# Tree # 2\n",
251+
"# the data type in different nodes can be different too\n",
243252
"[1, 2, 3, 3, 5 , 'my_Leaf']\n",
244253
"#hould return\n",
245254
"[('1', '2', '3'), ('1', '2', '5'), ('1', '3', 'my_Leaf')]"
@@ -255,7 +264,7 @@
255264
},
256265
{
257266
"cell_type": "code",
258-
"execution_count": 86,
267+
"execution_count": 93,
259268
"metadata": {},
260269
"outputs": [],
261270
"source": [
@@ -294,7 +303,7 @@
294303
},
295304
{
296305
"cell_type": "code",
297-
"execution_count": 87,
306+
"execution_count": 95,
298307
"metadata": {},
299308
"outputs": [
300309
{
@@ -355,7 +364,15 @@
355364
"source": [
356365
"# Your answer here\n",
357366
"\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."
359376
]
360377
},
361378
{
@@ -400,23 +417,20 @@
400417
"source": [
401418
"# Your answer here\n",
402419
"\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",
404421
"\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",
406423
"\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",
409426
"3. The last item added is the first one removed (Last In, First Out → LIFO)\n",
410427
"\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",
415429
"\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",
417431
"2. Take a node from the stack, check if it’s a leaf (has no children).\n",
418432
" - 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",
420434
"3. Repeat until we’ve visited all nodes.\n",
421435
"4. Since we use a stack, we process the left side first (because stacks work in Last In, First Out order)."
422436
]

0 commit comments

Comments
 (0)