|
21 | 21 | },
|
22 | 22 | {
|
23 | 23 | "cell_type": "code",
|
24 |
| - "execution_count": null, |
| 24 | + "execution_count": 202, |
25 | 25 | "metadata": {},
|
26 |
| - "outputs": [], |
| 26 | + "outputs": [ |
| 27 | + { |
| 28 | + "name": "stdout", |
| 29 | + "output_type": "stream", |
| 30 | + "text": [ |
| 31 | + "1\n" |
| 32 | + ] |
| 33 | + } |
| 34 | + ], |
27 | 35 | "source": [
|
28 |
| - "print((hash('your first name') % 3) + 1)" |
| 36 | + "print((hash('sarita') % 3) + 1)" |
29 | 37 | ]
|
30 | 38 | },
|
31 | 39 | {
|
|
72 | 80 | },
|
73 | 81 | {
|
74 | 82 | "cell_type": "code",
|
75 |
| - "execution_count": null, |
| 83 | + "execution_count": 203, |
76 | 84 | "metadata": {},
|
77 |
| - "outputs": [], |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "ename": "IndentationError", |
| 88 | + "evalue": "expected an indented block (2850872121.py, line 8)", |
| 89 | + "output_type": "error", |
| 90 | + "traceback": [ |
| 91 | + "\u001b[1;36m Cell \u001b[1;32mIn[203], line 8\u001b[1;36m\u001b[0m\n\u001b[1;33m # TODO\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n" |
| 92 | + ] |
| 93 | + } |
| 94 | + ], |
78 | 95 | "source": [
|
79 | 96 | "# Definition for a binary tree node.\n",
|
80 | 97 | "# class TreeNode(object):\n",
|
81 | 98 | "# def __init__(self, val = 0, left = None, right = None):\n",
|
82 | 99 | "# self.val = val\n",
|
83 | 100 | "# self.left = left\n",
|
84 | 101 | "# self.right = right\n",
|
85 |
| - "def is_symmetric(root: TreeNode) -> int:\n", |
| 102 | + "def is_duplicate(root: TreeNode) -> int:\n", |
86 | 103 | " # TODO"
|
87 | 104 | ]
|
88 | 105 | },
|
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [ |
| 111 | + { |
| 112 | + "data": { |
| 113 | + "text/plain": [ |
| 114 | + "2" |
| 115 | + ] |
| 116 | + }, |
| 117 | + "execution_count": 195, |
| 118 | + "metadata": {}, |
| 119 | + "output_type": "execute_result" |
| 120 | + } |
| 121 | + ], |
| 122 | + "source": [ |
| 123 | + "#Example1\n", |
| 124 | + "root = TreeNode(1)\n", |
| 125 | + "root.left = TreeNode(2)\n", |
| 126 | + "root.right = TreeNode(2)\n", |
| 127 | + "root.left.left = TreeNode(3)\n", |
| 128 | + "root.left.right = TreeNode(5)\n", |
| 129 | + "root.right.left = TreeNode(6)\n", |
| 130 | + "root.right.right = TreeNode(7)\n", |
| 131 | + "# print_in_order(root)\n", |
| 132 | + "is_duplicate(root)" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": null, |
| 138 | + "metadata": {}, |
| 139 | + "outputs": [ |
| 140 | + { |
| 141 | + "data": { |
| 142 | + "text/plain": [ |
| 143 | + "10" |
| 144 | + ] |
| 145 | + }, |
| 146 | + "execution_count": 196, |
| 147 | + "metadata": {}, |
| 148 | + "output_type": "execute_result" |
| 149 | + } |
| 150 | + ], |
| 151 | + "source": [ |
| 152 | + "#Example2\n", |
| 153 | + "root = TreeNode(1)\n", |
| 154 | + "root.left = TreeNode(10)\n", |
| 155 | + "root.right = TreeNode(2)\n", |
| 156 | + "root.left.left = TreeNode(3)\n", |
| 157 | + "root.left.right = TreeNode(10)\n", |
| 158 | + "root.right.left = TreeNode(12)\n", |
| 159 | + "root.right.right = TreeNode(12)\n", |
| 160 | + "# print_in_order(root)\n", |
| 161 | + "is_duplicate(root)" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "data": { |
| 171 | + "text/plain": [ |
| 172 | + "-1" |
| 173 | + ] |
| 174 | + }, |
| 175 | + "execution_count": 197, |
| 176 | + "metadata": {}, |
| 177 | + "output_type": "execute_result" |
| 178 | + } |
| 179 | + ], |
| 180 | + "source": [ |
| 181 | + "#Example3\n", |
| 182 | + "root = TreeNode(10)\n", |
| 183 | + "root.left = TreeNode(9)\n", |
| 184 | + "root.right = TreeNode(7)\n", |
| 185 | + "root.left.left = TreeNode(8)\n", |
| 186 | + "# print_in_order(root)\n", |
| 187 | + "is_duplicate(root)" |
| 188 | + ] |
| 189 | + }, |
89 | 190 | {
|
90 | 191 | "cell_type": "markdown",
|
91 | 192 | "metadata": {},
|
|
193 | 294 | ]
|
194 | 295 | },
|
195 | 296 | {
|
196 |
| - "cell_type": "code", |
197 |
| - "execution_count": null, |
| 297 | + "cell_type": "markdown", |
198 | 298 | "metadata": {},
|
199 |
| - "outputs": [], |
200 | 299 | "source": [
|
201 |
| - "# Your answer here" |
| 300 | + "# Your answer here\n", |
| 301 | + "We need to traverse a binary tree and find any duplicate node values. \n", |
| 302 | + " In case of multiple duplicates, the duplicate values closest to the root(Would be the first found duplicate in BFS) should be returned.\n", |
| 303 | + "So, essentially we need to perform the breadth-first serach and return the first found duplicate value" |
202 | 304 | ]
|
203 | 305 | },
|
204 | 306 | {
|
|
213 | 315 | "cell_type": "code",
|
214 | 316 | "execution_count": null,
|
215 | 317 | "metadata": {},
|
216 |
| - "outputs": [], |
| 318 | + "outputs": [ |
| 319 | + { |
| 320 | + "data": { |
| 321 | + "text/plain": [ |
| 322 | + "2" |
| 323 | + ] |
| 324 | + }, |
| 325 | + "execution_count": 198, |
| 326 | + "metadata": {}, |
| 327 | + "output_type": "execute_result" |
| 328 | + } |
| 329 | + ], |
| 330 | + "source": [ |
| 331 | + "# Your answer here\n", |
| 332 | + "# Example 1 Input: root = [10, 7, 2, 7, 3, 5, 2], output = 2\n", |
| 333 | + "root = TreeNode(4)\n", |
| 334 | + "root.left = TreeNode(7)\n", |
| 335 | + "root.right = TreeNode(2)\n", |
| 336 | + "root.left.left = TreeNode(11)\n", |
| 337 | + "root.left.right = TreeNode(12)\n", |
| 338 | + "root.right.left = TreeNode(5)\n", |
| 339 | + "root.right.left.right = TreeNode(2)\n", |
| 340 | + "root.right.left.left = TreeNode(14)\n", |
| 341 | + "# print_in_order(root)\n", |
| 342 | + "is_duplicate(root)" |
| 343 | + ] |
| 344 | + }, |
| 345 | + { |
| 346 | + "cell_type": "code", |
| 347 | + "execution_count": null, |
| 348 | + "metadata": {}, |
| 349 | + "outputs": [ |
| 350 | + { |
| 351 | + "data": { |
| 352 | + "text/plain": [ |
| 353 | + "10" |
| 354 | + ] |
| 355 | + }, |
| 356 | + "execution_count": 199, |
| 357 | + "metadata": {}, |
| 358 | + "output_type": "execute_result" |
| 359 | + } |
| 360 | + ], |
217 | 361 | "source": [
|
218 |
| - "# Your answer here" |
| 362 | + "# Example 2 Input: root = [1, 5, 7, 2, 2, 3, 7] Output =10\n", |
| 363 | + "root = TreeNode(10)\n", |
| 364 | + "root.left = TreeNode(7)\n", |
| 365 | + "root.right = TreeNode(2)\n", |
| 366 | + "root.left.left = TreeNode(1)\n", |
| 367 | + "root.left.right = TreeNode(3)\n", |
| 368 | + "root.right.left = TreeNode(5)\n", |
| 369 | + "root.right.right = TreeNode(18)\n", |
| 370 | + "root.right.left.left = TreeNode(12)\n", |
| 371 | + "root.right.right.left = TreeNode(10)\n", |
| 372 | + "# print_in_order(root)\n", |
| 373 | + "is_duplicate(root)" |
219 | 374 | ]
|
220 | 375 | },
|
221 | 376 | {
|
|
232 | 387 | "metadata": {},
|
233 | 388 | "outputs": [],
|
234 | 389 | "source": [
|
235 |
| - "# Your answer here" |
| 390 | + "# Your answer here\n", |
| 391 | + "# Definition for a binary tree node.\n", |
| 392 | + "class TreeNode(object):\n", |
| 393 | + " def __init__(self, val = 0, left = None, right = None):\n", |
| 394 | + " self.val = val\n", |
| 395 | + " self.left = left\n", |
| 396 | + " self.right = right\n", |
| 397 | + "\n", |
| 398 | + "def is_duplicate(root: TreeNode) -> int:\n", |
| 399 | + " if not root:\n", |
| 400 | + " return -1\n", |
| 401 | + " queue = deque([root])\n", |
| 402 | + " seen_values = set() \n", |
| 403 | + " while queue:\n", |
| 404 | + " node = queue.popleft()\n", |
| 405 | + " if node.val in seen_values:\n", |
| 406 | + " # if value is seen already return as duplicate \n", |
| 407 | + " return node.val\n", |
| 408 | + " seen_values.add(node.val)\n", |
| 409 | + " if node.left:\n", |
| 410 | + " queue.append(node.left)\n", |
| 411 | + " if node.right:\n", |
| 412 | + " queue.append(node.right)\n", |
| 413 | + " return -1" |
236 | 414 | ]
|
237 | 415 | },
|
238 | 416 | {
|
|
244 | 422 | ]
|
245 | 423 | },
|
246 | 424 | {
|
247 |
| - "cell_type": "code", |
248 |
| - "execution_count": null, |
| 425 | + "cell_type": "markdown", |
249 | 426 | "metadata": {},
|
250 |
| - "outputs": [], |
251 | 427 | "source": [
|
252 |
| - "# Your answer here" |
| 428 | + "# Your answer here\n", |
| 429 | + "While traversing the nodes, we maintain a set of unique values that we come across. If we encounter a duplicate, we return that value. In case it is a new value, we add it to the set.\n", |
| 430 | + "The deque function helps us to create a queue which we then access from the left, to process the node values sequentially." |
253 | 431 | ]
|
254 | 432 | },
|
255 | 433 | {
|
|
261 | 439 | ]
|
262 | 440 | },
|
263 | 441 | {
|
264 |
| - "cell_type": "code", |
265 |
| - "execution_count": null, |
| 442 | + "cell_type": "markdown", |
266 | 443 | "metadata": {},
|
267 |
| - "outputs": [], |
268 | 444 | "source": [
|
269 |
| - "# Your answer here" |
| 445 | + "# Your answer here\n", |
| 446 | + "Time complexity - In our solution code, each node is visited once , creating a time complexity of O(n)\n", |
| 447 | + "\n", |
| 448 | + "Space Complexity - The worst case for a highly skewed tree would have a space complexity of O(n)" |
270 | 449 | ]
|
271 | 450 | },
|
272 | 451 | {
|
|
278 | 457 | ]
|
279 | 458 | },
|
280 | 459 | {
|
281 |
| - "cell_type": "code", |
282 |
| - "execution_count": null, |
| 460 | + "cell_type": "markdown", |
283 | 461 | "metadata": {},
|
284 |
| - "outputs": [], |
285 | 462 | "source": [
|
286 |
| - "# Your answer here" |
| 463 | + "# Your answer here\n", |
| 464 | + "I was thinking of using a recursive function\n", |
| 465 | + "\n", |
| 466 | + "The function would need two parameters, the node value and the seen list to keep track of the already visited values\n", |
| 467 | + "\n", |
| 468 | + "Under a function is_duplicate, do below:\n", |
| 469 | + "\n", |
| 470 | + "create an empty set to store unique values\n", |
| 471 | + "\n", |
| 472 | + "Evaluate the first node\n", |
| 473 | + "if the node value is encountered the first time, add it to the set\n", |
| 474 | + "otherwise, when encountering a value that already exists in set, return the value as a duplicate(and exit the function)\n", |
| 475 | + "\n", |
| 476 | + "continue evaluating the left and right branches and call the is_duplicate function recursively with the left node value and the set in it's current state\n", |
| 477 | + "\n", |
| 478 | + "\n" |
287 | 479 | ]
|
288 | 480 | },
|
289 | 481 | {
|
|
345 | 537 | "name": "python",
|
346 | 538 | "nbconvert_exporter": "python",
|
347 | 539 | "pygments_lexer": "ipython3",
|
348 |
| - "version": "3.11.7" |
| 540 | + "version": "3.9.15" |
349 | 541 | }
|
350 | 542 | },
|
351 | 543 | "nbformat": 4,
|
|
0 commit comments