Skip to content

Commit 9ed67f6

Browse files
committed
Comments added
1 parent 595676e commit 9ed67f6

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

02_activities/assignments/assignment_1.ipynb

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,108 +94,124 @@
9494
"source": [
9595
"# Definition for a binary tree node.\n",
9696
"class TreeNode(object):\n",
97-
" def __init__(self, val = 0, left = None, right = None):\n",
97+
" def __init__(self, val=0, left=None, right=None):\n",
9898
" self.val = val\n",
9999
" self.left = left\n",
100100
" self.right = right\n",
101101
"\n",
102-
"# Building the tree(DFS-pre-order)\n",
102+
"# Building the tree(DFS_pre-order)\n",
103103
"def build_tree(roots):\n",
104104
" if not roots:\n",
105105
" return None\n",
106-
" \n",
106+
"\n",
107107
" root = TreeNode(roots[0])\n",
108108
" built_tree = [root]\n",
109109
" index = 1\n",
110-
" \n",
110+
"\n",
111111
" while built_tree and index < len(roots):\n",
112112
" node = built_tree.pop(0)\n",
113-
" \n",
113+
"\n",
114114
" if index < len(roots) and roots[index] is not None:\n",
115115
" node.left = TreeNode(roots[index])\n",
116116
" built_tree.append(node.left)\n",
117117
" index += 1\n",
118-
" \n",
118+
"\n",
119119
" if index < len(roots) and roots[index] is not None:\n",
120120
" node.right = TreeNode(roots[index])\n",
121121
" built_tree.append(node.right)\n",
122122
" index += 1\n",
123-
" \n",
123+
"\n",
124124
" return root\n",
125125
"\n",
126-
"def dfs(node, depth, visited):\n",
126+
"# DFS function to find duplicates and their depths\n",
127+
"def dfs(node, depth, visited, closest_duplicate, min_depth):\n",
127128
" if node is None:\n",
128-
" return None, float('inf') # No duplicate, infinite depth\n",
129-
"\n",
130-
" # If the node's value has already been visited, it's a duplicate\n",
129+
" return closest_duplicate, min_depth # No duplicates found in this path\n",
130+
" \n",
131131
" if node.val in visited:\n",
132-
" return node.val, depth # Found a duplicate at this depth\n",
132+
" if depth < min_depth:\n",
133+
" closest_duplicate = node.val\n",
134+
" min_depth = depth\n",
133135
" \n",
134136
" visited.add(node.val)\n",
135137
"\n",
136-
" # Recurse on left and right children and return the closest duplicate\n",
137-
" left_val, left_depth = dfs(node.left, depth + 1, visited)\n",
138-
" right_val, right_depth = dfs(node.right, depth + 1, visited)\n",
138+
" closest_duplicate, min_depth = dfs(node.left, depth + 1, visited, closest_duplicate, min_depth)\n",
139+
" closest_duplicate, min_depth = dfs(node.right, depth + 1, visited, closest_duplicate, min_depth)\n",
139140
"\n",
140-
" # Compare which duplicate is closer to the root\n",
141-
" if left_depth < right_depth:\n",
142-
" return left_val, left_depth\n",
143-
" else:\n",
144-
" return right_val, right_depth\n",
141+
" return closest_duplicate, min_depth\n",
145142
"\n",
146143
"\n",
144+
"# Function to find the duplicate with the smallest depth (closest to the root)\n",
147145
"def find_duplicate(root):\n",
148-
" visited = set() # To track visited node values\n",
149-
" closest_duplicate, min_depth = dfs(root, 0, visited)\n",
150-
" \n",
151-
" # Return the closest duplicate if found, otherwise return -1\n",
152-
" return closest_duplicate if closest_duplicate is not None else -1\n",
153-
"\n"
146+
" visited = set() \n",
147+
" closest_duplicate, min_depth = dfs(root, 0, visited, None, float('inf')) # setting root as the node(depth 0),closest_duplicate as None, and min_depth as infinite as everything will be less than that\n",
148+
"\n",
149+
" return closest_duplicate if closest_duplicate is not None else -1\n"
154150
]
155151
},
156152
{
157153
"cell_type": "code",
158-
"execution_count": 21,
154+
"execution_count": 45,
159155
"metadata": {},
160156
"outputs": [
161157
{
162158
"name": "stdout",
163159
"output_type": "stream",
164160
"text": [
165-
"Duplicate value: 2\n"
161+
"Duplicate value: -1\n"
166162
]
167163
}
168164
],
169165
"source": [
170-
"example_roots = [1, 2, 2, 3, 5, 6, 7] # Testing the example 1\n",
166+
"example_roots = [1, 2, 2, 3, 5, 6, 7] # Testing example 1\n",
171167
"root = build_tree(example_roots)\n",
172168
"duplicate = find_duplicate(root)\n",
173169
"print(f\"Duplicate value: {duplicate}\") "
174170
]
175171
},
176172
{
177173
"cell_type": "code",
178-
"execution_count": 22,
174+
"execution_count": 39,
179175
"metadata": {},
180176
"outputs": [
181177
{
182178
"name": "stdout",
183179
"output_type": "stream",
184180
"text": [
185-
"Duplicate value: 12\n"
181+
"Duplicate value: 10\n"
182+
]
183+
}
184+
],
185+
"source": [
186+
"example_roots = [1, 10, 2, 3, 10, 12, 12] # Testing example 2\n",
187+
"root = build_tree(example_roots)\n",
188+
"duplicate = find_duplicate(root)\n",
189+
"print(f\"Duplicate value: {duplicate}\") "
190+
]
191+
},
192+
{
193+
"cell_type": "code",
194+
"execution_count": 40,
195+
"metadata": {},
196+
"outputs": [
197+
{
198+
"name": "stdout",
199+
"output_type": "stream",
200+
"text": [
201+
"Duplicate value: -1\n"
186202
]
187203
}
188204
],
189205
"source": [
190-
"example_roots = [1, 10, 2, 3, 10, 12, 12] # Testing the example 3\n",
206+
"example_roots = [10, 9, 8, 7] # Testing example 3\n",
191207
"root = build_tree(example_roots)\n",
192208
"duplicate = find_duplicate(root)\n",
193209
"print(f\"Duplicate value: {duplicate}\") "
194210
]
195211
},
196212
{
197213
"cell_type": "code",
198-
"execution_count": 17,
214+
"execution_count": 41,
199215
"metadata": {},
200216
"outputs": [
201217
{

0 commit comments

Comments
 (0)