|
88 | 88 | },
|
89 | 89 | {
|
90 | 90 | "cell_type": "code",
|
91 |
| - "execution_count": null, |
| 91 | + "execution_count": 20, |
92 | 92 | "metadata": {},
|
93 | 93 | "outputs": [],
|
94 | 94 | "source": [
|
|
151 | 151 | },
|
152 | 152 | {
|
153 | 153 | "cell_type": "code",
|
154 |
| - "execution_count": 45, |
| 154 | + "execution_count": 21, |
155 | 155 | "metadata": {},
|
156 | 156 | "outputs": [
|
157 | 157 | {
|
158 | 158 | "name": "stdout",
|
159 | 159 | "output_type": "stream",
|
160 | 160 | "text": [
|
161 |
| - "Duplicate value: -1\n" |
| 161 | + "Duplicate value: 2\n" |
162 | 162 | ]
|
163 | 163 | }
|
164 | 164 | ],
|
|
171 | 171 | },
|
172 | 172 | {
|
173 | 173 | "cell_type": "code",
|
174 |
| - "execution_count": 39, |
| 174 | + "execution_count": 22, |
175 | 175 | "metadata": {},
|
176 | 176 | "outputs": [
|
177 | 177 | {
|
|
191 | 191 | },
|
192 | 192 | {
|
193 | 193 | "cell_type": "code",
|
194 |
| - "execution_count": 40, |
| 194 | + "execution_count": 23, |
195 | 195 | "metadata": {},
|
196 | 196 | "outputs": [
|
197 | 197 | {
|
|
209 | 209 | "print(f\"Duplicate value: {duplicate}\") "
|
210 | 210 | ]
|
211 | 211 | },
|
212 |
| - { |
213 |
| - "cell_type": "code", |
214 |
| - "execution_count": 41, |
215 |
| - "metadata": {}, |
216 |
| - "outputs": [ |
217 |
| - { |
218 |
| - "name": "stdout", |
219 |
| - "output_type": "stream", |
220 |
| - "text": [ |
221 |
| - "Duplicate value: -1\n" |
222 |
| - ] |
223 |
| - } |
224 |
| - ], |
225 |
| - "source": [ |
226 |
| - "example_roots = [] # Testing empty root\n", |
227 |
| - "root = build_tree(example_roots)\n", |
228 |
| - "duplicate = find_duplicate(root)\n", |
229 |
| - "print(f\"Duplicate value: {duplicate}\") " |
230 |
| - ] |
231 |
| - }, |
232 | 212 | {
|
233 | 213 | "cell_type": "markdown",
|
234 | 214 | "metadata": {},
|
|
336 | 316 | ]
|
337 | 317 | },
|
338 | 318 | {
|
339 |
| - "cell_type": "code", |
340 |
| - "execution_count": null, |
| 319 | + "cell_type": "markdown", |
341 | 320 | "metadata": {},
|
342 |
| - "outputs": [], |
343 | 321 | "source": [
|
344 |
| - "# Your answer here" |
| 322 | + "It is expected to check if there are duplicates on the given Binary Tree or not. \n", |
| 323 | + "Input: Root of the tree\n", |
| 324 | + "Output: \n", |
| 325 | + "- If there is no duplicate: -1\n", |
| 326 | + "- If there is a duplicates: duplicated node\n", |
| 327 | + "- If there are multiple duplicates: duplicated node nearest to root" |
345 | 328 | ]
|
346 | 329 | },
|
347 | 330 | {
|
|
351 | 334 | "- In this .ipynb file, there are examples that illustrate how the code should work (the examples provided above). Create 2 new examples for the question you have been assigned, that demonstrate you understand the problem. For question 1 and 2, you don't need to create the tree demonstration, just the input and output.\n"
|
352 | 335 | ]
|
353 | 336 | },
|
| 337 | + { |
| 338 | + "cell_type": "markdown", |
| 339 | + "metadata": {}, |
| 340 | + "source": [ |
| 341 | + "additional_example_1: root = [1,2,4,11,7,11,5,13,15,12,12,7,8] # multiple duplicates at different levels\n", |
| 342 | + "additional_example_2: root = [1,2,3,2,5,7,2] # multiple same duplicates at several levels\n", |
| 343 | + "additionak_example_3: root=[] # emplty root\n", |
| 344 | + "(Refer to the below test results)" |
| 345 | + ] |
| 346 | + }, |
| 347 | + { |
| 348 | + "cell_type": "code", |
| 349 | + "execution_count": 25, |
| 350 | + "metadata": {}, |
| 351 | + "outputs": [ |
| 352 | + { |
| 353 | + "name": "stdout", |
| 354 | + "output_type": "stream", |
| 355 | + "text": [ |
| 356 | + "Duplicate value: 11\n" |
| 357 | + ] |
| 358 | + } |
| 359 | + ], |
| 360 | + "source": [ |
| 361 | + "example_roots = [1,2,4,11,7,11,5,13,15,12,12,7,8] # Testing additional_example_1\n", |
| 362 | + "root = build_tree(example_roots)\n", |
| 363 | + "duplicate = find_duplicate(root)\n", |
| 364 | + "print(f\"Duplicate value: {duplicate}\")" |
| 365 | + ] |
| 366 | + }, |
354 | 367 | {
|
355 | 368 | "cell_type": "code",
|
356 | 369 | "execution_count": null,
|
357 | 370 | "metadata": {},
|
358 |
| - "outputs": [], |
| 371 | + "outputs": [ |
| 372 | + { |
| 373 | + "name": "stdout", |
| 374 | + "output_type": "stream", |
| 375 | + "text": [ |
| 376 | + "Duplicate value: 2\n" |
| 377 | + ] |
| 378 | + } |
| 379 | + ], |
359 | 380 | "source": [
|
360 |
| - "# Your answer here" |
| 381 | + "example_roots = [1,2,3,2,5,7,2] # Testing additional_example_2\n", |
| 382 | + "root = build_tree(example_roots)\n", |
| 383 | + "duplicate = find_duplicate(root)\n", |
| 384 | + "print(f\"Duplicate value: {duplicate}\")" |
| 385 | + ] |
| 386 | + }, |
| 387 | + { |
| 388 | + "cell_type": "code", |
| 389 | + "execution_count": 27, |
| 390 | + "metadata": {}, |
| 391 | + "outputs": [ |
| 392 | + { |
| 393 | + "name": "stdout", |
| 394 | + "output_type": "stream", |
| 395 | + "text": [ |
| 396 | + "Duplicate value: -1\n" |
| 397 | + ] |
| 398 | + } |
| 399 | + ], |
| 400 | + "source": [ |
| 401 | + "example_roots = [] # Testing additional_exaple_3\n", |
| 402 | + "root = build_tree(example_roots)\n", |
| 403 | + "duplicate = find_duplicate(root)\n", |
| 404 | + "print(f\"Duplicate value: {duplicate}\") " |
361 | 405 | ]
|
362 | 406 | },
|
363 | 407 | {
|
|
369 | 413 | ]
|
370 | 414 | },
|
371 | 415 | {
|
372 |
| - "cell_type": "code", |
373 |
| - "execution_count": null, |
| 416 | + "cell_type": "markdown", |
374 | 417 | "metadata": {},
|
375 |
| - "outputs": [], |
376 | 418 | "source": [
|
377 |
| - "# Your answer here" |
| 419 | + "Code is provided above under \"Source Code for Question 1\" section. " |
378 | 420 | ]
|
379 | 421 | },
|
380 | 422 | {
|
|
386 | 428 | ]
|
387 | 429 | },
|
388 | 430 | {
|
389 |
| - "cell_type": "code", |
390 |
| - "execution_count": null, |
| 431 | + "cell_type": "markdown", |
391 | 432 | "metadata": {},
|
392 |
| - "outputs": [], |
393 | 433 | "source": [
|
394 |
| - "# Your answer here" |
| 434 | + "In the above code as a solution to Question 1 there are two major functions:\n", |
| 435 | + "- Build_tree \n", |
| 436 | + "- dfs (dfs recuirsion)\n", |
| 437 | + "First we build the tree by having the input as an array of nodes and then we use dfs fucntion to find the duplicate(s) considering their depth. The solution is considering both the duplicate and the depth so that it can provide the desired results." |
395 | 438 | ]
|
396 | 439 | },
|
397 | 440 | {
|
|
403 | 446 | ]
|
404 | 447 | },
|
405 | 448 | {
|
406 |
| - "cell_type": "code", |
407 |
| - "execution_count": null, |
| 449 | + "cell_type": "markdown", |
408 | 450 | "metadata": {},
|
409 |
| - "outputs": [], |
410 | 451 | "source": [
|
411 |
| - "# Your answer here" |
| 452 | + "Time complexity:\n", |
| 453 | + "- Build_tree: O(n) where \"n\" is the number of nodes.\n", |
| 454 | + "- dfs: O(n)\n", |
| 455 | + "Space complexity:\n", |
| 456 | + "- Visited set: O(n)\n", |
| 457 | + "- dfs recursion: O(log(n))\n", |
| 458 | + "So, overal time complexity of the code is O(n) as every node is visited once and the overal space complexity is also O(n) in the worst case which is an unbalanced tree considering the recursion stack and the space required for \"visited\" set. " |
412 | 459 | ]
|
413 | 460 | },
|
414 | 461 | {
|
|
420 | 467 | ]
|
421 | 468 | },
|
422 | 469 | {
|
423 |
| - "cell_type": "code", |
424 |
| - "execution_count": null, |
| 470 | + "cell_type": "markdown", |
425 | 471 | "metadata": {},
|
426 |
| - "outputs": [], |
427 | 472 | "source": [
|
428 |
| - "# Your answer here" |
| 473 | + "An alternative solution to the Question 1 problem could be using a BFS instead of DFS since that process nodes level by level which may have a better impact on efficiency." |
429 | 474 | ]
|
430 | 475 | },
|
431 | 476 | {
|
|
0 commit comments