Skip to content

Commit

Permalink
r
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsentabibian committed Nov 14, 2023
1 parent fee60db commit 39aff16
Show file tree
Hide file tree
Showing 525 changed files with 122,240 additions and 0 deletions.
211 changes: 211 additions & 0 deletions docs/01. Array Hashing/1. Two Sum.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d6161252-e815-48d5-a2a3-5444c12e3b49",
"metadata": {},
"source": [
"# 100: Same Tree\n",
"\n",
"**Difficulty:** Easy\n",
"\n",
"**Link to Problem:** [To see the Same Tree problem on LeetCode, click here!](https://leetcode.com/problems/same-tree/)\n",
"\n",
"---\n",
"Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.\n",
"\n",
"Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.\n",
"\n",
"**Constraints**\n",
"\n",
"1. The number of nodes in both trees is in the range `[0, 100]`.\n",
"2. $-10^4$ <= `Node.val` <= $10^4$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "aabf7095-07dc-4ab7-876c-ec7213ed0de5",
"metadata": {
"trusted": true
},
"outputs": [],
"source": [
"# Definition for a binary tree node.\n",
"class TreeNode:\n",
" def __init__(self, val=0, left=None, right=None):\n",
" # Initialize a TreeNode with a value (val), left child, and right child.\n",
" self.val = val\n",
" self.left = left\n",
" self.right = right\n",
"\n",
"def isSameTree(p, q):\n",
" # Base case: If both p and q are None, the trees are the same.\n",
" if not p and not q:\n",
" return True\n",
" \n",
" # Base case: If either p or q is None (but not both), the trees are different.\n",
" if not p or not q:\n",
" return False\n",
" \n",
" # Check if the values of the current nodes (p.val and q.val) are equal.\n",
" if p.val != q.val:\n",
" return False\n",
" \n",
" # Recursively check the left and right subtrees of p and q.\n",
" # If both subtrees are the same, the entire trees are the same.\n",
" return isSameTree(p.left, q.left) and isSameTree(p.right, q.right)"
]
},
{
"cell_type": "markdown",
"id": "ece39324-cd9e-47d7-8677-a001878494d9",
"metadata": {},
"source": [
"In this code, we define a `TreeNode` class to represent binary tree nodes and a `isSameTree` function to check if two binary trees are the same. The function uses recursive traversal to compare the trees' structures and values.\n",
"\n",
"1. We start by defining a `TreeNode` class, which represents a node in a binary tree. Each node has a `val` (the node's value), a `left` child, and a right child. This class will help us create and work with binary trees.\n",
"2. Next, we define the `isSameTree` function, which checks if two binary trees (`p` and `q`) are the same.\n",
" + The base case for the recursion is when both `p` and `q` are `None`. In this case, they are considered the same, so we return `True`.\n",
" + If either `p` or `q` is `None` (but not both), they cannot be the same, so we return `False`.\n",
" + If the values of the current nodes `p.val` and `q.val` are not equal, we return `False` because the trees cannot be the same.\n",
" + Finally, we recursively check the left and right subtrees of `p` and `q` to see if they are the same."
]
},
{
"cell_type": "markdown",
"id": "a54ea5b7-c53f-46c0-a44f-ef769ac68244",
"metadata": {},
"source": [
"## Test cases"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "355f5bfd-94e1-4c5b-9914-cc20775f4160",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"### Example 1\n",
"\n",
"#Input: `p = [1,2,3]`, `q = [1,2,3]`\n",
"\n",
"p1 = TreeNode(1, TreeNode(2), TreeNode(3))\n",
"q1 = TreeNode(1, TreeNode(2), TreeNode(3))\n",
"print(isSameTree(p1, q1)) "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "192912fb-9d12-4911-9a4f-bfd37043e11d",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"### Example 2\n",
"\n",
"#Input: `p = [1,2]`, `q = [1,null,2]`\n",
"\n",
"p2 = TreeNode(1, TreeNode(2), None)\n",
"q2 = TreeNode(1, None, TreeNode(2))\n",
"print(isSameTree(p2, q2))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5897c29d-26f8-486a-878c-43c09ff25ce4",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"### Example 3\n",
"\n",
"#Input: p = [1,2,1], q = [1,1,2]\n",
"\n",
"p3 = TreeNode(1, TreeNode(2), TreeNode(1))\n",
"q3 = TreeNode(1, TreeNode(1), TreeNode(2))\n",
"print(isSameTree(p3, q3))"
]
},
{
"cell_type": "markdown",
"id": "7d9dd448-c5f5-4192-8aa5-7c70ec7f42e2",
"metadata": {},
"source": [
"## Time and Space Complexity Analysis\n",
"\n",
"**Time Complexity**\n",
"\n",
"The time complexity of the `isSameTree` function can be analyzed as follows:\n",
"\n",
"In the worst case, the function needs to visit every node in both trees once to determine if they are the same.\n",
"Since each node is visited exactly once, the time complexity is $O(n)$, where $n$ is the total number of nodes in the input trees.\n",
"\n",
"**Space Complexity**\n",
"The space complexity of the `isSameTree` function can be analyzed as follows:\n",
"\n",
"The space used by the function's call stack during recursion is proportional to the maximum depth of the binary trees.\n",
"In the worst case, when the trees are completely unbalanced (all nodes form a single branch), the maximum depth will be $n$, where $n$ is the total number of nodes in the input trees.\n",
"Therefore, the space complexity is $O(n)$ due to the recursive call stack.\n",
"In addition to the call stack, there is a small constant amount of space used for variables and comparisons within each recursive call, but this space is not significant in terms of the overall space complexity.\n",
"\n",
"**In summary:**\n",
"\n",
"+ Time Complexity: $O(n)$ where $n$ is the total number of nodes in the input trees.\n",
"+ Space Complexity: $O(n)$ due to the recursive call stack."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (Pyodide)",
"language": "python",
"name": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 39aff16

Please sign in to comment.