Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 1 #29

Closed
wants to merge 10 commits into from
115 changes: 102 additions & 13 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
{
"cells": [
{
Expand All @@ -21,11 +22,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print((hash('your first name') % 3) + 1)"
"print((hash('shabiga') % 3) + 1)"
]
},
{
Expand Down Expand Up @@ -198,7 +207,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#Need to determine whether the root of a binary tree includes a duplicate value. If there is a duplicate, then need to return that value. If there are numerous duplicates, then have to return to closest to the root. If there are no duplicates then return -1. "
]
},
{
Expand All @@ -211,11 +221,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#Input: root = [3, 5, 2, 1, 2, 6, 2]\n",
"#Output: 2\n",
"\n",
"#Input: root = [5, 3, 8, 1, 4, 7, 9]\n",
"#Output: -1\n"
]
},
{
Expand All @@ -228,11 +243,79 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"-1\n"
]
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"from collections import deque\n",
"\n",
"class TreeNode:\n",
" def __init__(self, val=0, left=None, right=None):\n",
" self.val = val\n",
" self.left = left\n",
" self.right = right\n",
"\n",
"def is_duplicate(root: TreeNode) -> int:\n",
" if not root:\n",
" return -1\n",
" \n",
" queue = deque([root])\n",
" seen = set()\n",
" \n",
" while queue:\n",
" node = queue.popleft()\n",
" \n",
" if node.val in seen:\n",
" return node.val\n",
" seen.add(node.val)\n",
" \n",
" if node.left:\n",
" queue.append(node.left)\n",
" \n",
" if node.right:\n",
" queue.append(node.right)\n",
" \n",
" return -1 \n",
"\n",
"# 3\n",
"# / \\\n",
"# 5 2\n",
"# / \\ / \\\n",
"# 1 2 6 2\n",
"root1 = TreeNode(3)\n",
"root1.left = TreeNode(5)\n",
"root1.right = TreeNode(2)\n",
"root1.left.left = TreeNode(1)\n",
"root1.left.right = TreeNode(2)\n",
"root1.right.left = TreeNode(6)\n",
"root1.right.right = TreeNode(2)\n",
"print(is_duplicate(root1))\n",
"\n",
"# 5\n",
"# / \\\n",
"# 3 8\n",
"# / \\ / \\\n",
"# 1 4 7 9\n",
"root2 = TreeNode(5)\n",
"root2.left = TreeNode(3)\n",
"root2.right = TreeNode(8)\n",
"root2.left.left = TreeNode(1)\n",
"root2.left.right = TreeNode(4)\n",
"root2.right.left = TreeNode(7)\n",
"root2.right.right = TreeNode(9)\n",
"print(is_duplicate(root2)) \n",
"\n",
"\n"
]
},
{
Expand All @@ -249,7 +332,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#The purpose of the is_duplicate function is to locate the binary tree's first duplicate integer. Beginning at the top of the tree, it goes through every number (or node) one by one. It uses a set to keep track of the numbers it has already seen while it examines each one. In this manner, it can recognize a duplication when it comes across a number it has already seen. As soon as it finds and returns the first duplicate number, the function ends. It returns -1 if, after verifying every number in the tree, no duplicates are discovered. With this method, the function's performance is mostly determined by the number of nodes in the tree, ensuring that it functions effectively even for big trees."
]
},
{
Expand All @@ -266,7 +350,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#The is_duplicate function determines whether a number occurs more than once in a binary tree. It employs a technique where it looks up each number once and utilizes a list to remember which numbers it has already seen. In this manner, it may identify a duplicate if it comes across a number it has already seen. As soon as it locates and returns the first duplicate number, the function ends. The function returns a special value (-1) if there are no duplicates. Because it only examines each number once and utilizes memory proportionate to the number of numbers in the tree, this approach is effective for trees of any size."
]
},
{
Expand All @@ -283,7 +368,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"#A different method involves repeatedly visiting every node in a binary tree in order to identify duplicate values using Depth First Search (DFS) with Inorder, Preorder, or Postorder traversal. Keep track of visited values by maintaining a set during the traversal. Return the value of a node if it is already in the set; else, it denotes a duplicate. If not, add the node's value to the set and carry on recursively going through the tree on the left and right."
]
},
{
Expand Down Expand Up @@ -345,9 +431,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
=======

>>>>>>> 422fc1e2dd4bada2434f5d03f395f063b07f47c6
Loading