Skip to content

Commit

Permalink
l
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsentabibian committed Nov 14, 2023
1 parent 2cc67ee commit 7a55649
Show file tree
Hide file tree
Showing 445 changed files with 107,787 additions and 53 deletions.
106 changes: 53 additions & 53 deletions Book.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/01. Array Hashing/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/02. Two Pointers/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/04. Stack/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/05. Binary Search/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/06. Linked List/README.doctree
Binary file not shown.
Binary file added _build/.doctrees/07. Trees/100. Same Tree.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/07. Trees/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/08. Tries/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/10. Backtracking/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/11. Graphs/README.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/15. Greedy/README.doctree
Binary file not shown.
Binary file added _build/.doctrees/16. Intervals/README.doctree
Binary file not shown.
Binary file added _build/.doctrees/17. Math Geometry/README.doctree
Binary file not shown.
Binary file not shown.
Binary file added _build/.doctrees/Book.doctree
Binary file not shown.
Binary file added _build/.doctrees/CODE_OF_CONDUCT.doctree
Binary file not shown.
Binary file added _build/.doctrees/CONTRIBUTING.doctree
Binary file not shown.
Binary file added _build/.doctrees/README.doctree
Binary file not shown.
Binary file added _build/.doctrees/SECURITY.doctree
Binary file not shown.
Binary file added _build/.doctrees/environment.pickle
Binary file not shown.
4 changes: 4 additions & 0 deletions _build/html/.buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 8b102b6ce68f3bc9f65a9c18adf2ae17
tags: 645f666f9bcd5a90fca523b33c5a78b7
720 changes: 720 additions & 0 deletions _build/html/01. Array Hashing/1. Two Sum.html

Large diffs are not rendered by default.

714 changes: 714 additions & 0 deletions _build/html/01. Array Hashing/128. Longest Consecutive Sequence.html

Large diffs are not rendered by default.

721 changes: 721 additions & 0 deletions _build/html/01. Array Hashing/217. Contains Duplicate.html

Large diffs are not rendered by default.

717 changes: 717 additions & 0 deletions _build/html/01. Array Hashing/238. Product of Array Except Self.html

Large diffs are not rendered by default.

708 changes: 708 additions & 0 deletions _build/html/01. Array Hashing/242. Valid Anagram.html

Large diffs are not rendered by default.

721 changes: 721 additions & 0 deletions _build/html/01. Array Hashing/347. Top K Frequent Elements.html

Large diffs are not rendered by default.

735 changes: 735 additions & 0 deletions _build/html/01. Array Hashing/49. Group Anagrams.html

Large diffs are not rendered by default.

728 changes: 728 additions & 0 deletions _build/html/01. Array Hashing/659. Encode and Decode Strings.html

Large diffs are not rendered by default.

640 changes: 640 additions & 0 deletions _build/html/01. Array Hashing/README.html

Large diffs are not rendered by default.

728 changes: 728 additions & 0 deletions _build/html/02. Two Pointers/11. Container With Most Water.html

Large diffs are not rendered by default.

728 changes: 728 additions & 0 deletions _build/html/02. Two Pointers/125. Valid Palindrome.html

Large diffs are not rendered by default.

751 changes: 751 additions & 0 deletions _build/html/02. Two Pointers/15. 3Sum.html

Large diffs are not rendered by default.

623 changes: 623 additions & 0 deletions _build/html/02. Two Pointers/README.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

761 changes: 761 additions & 0 deletions _build/html/03. Sliding Window/76. Minimum Window Substring.html

Large diffs are not rendered by default.

626 changes: 626 additions & 0 deletions _build/html/03. Sliding Window/README.html

Large diffs are not rendered by default.

723 changes: 723 additions & 0 deletions _build/html/04. Stack/20. Valid Parentheses.html

Large diffs are not rendered by default.

617 changes: 617 additions & 0 deletions _build/html/04. Stack/README.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

739 changes: 739 additions & 0 deletions _build/html/05. Binary Search/33. Search in Rotated Sorted Array.html

Large diffs are not rendered by default.

620 changes: 620 additions & 0 deletions _build/html/05. Binary Search/README.html

Large diffs are not rendered by default.

755 changes: 755 additions & 0 deletions _build/html/06. Linked List/141. Linked List Cycle.html

Large diffs are not rendered by default.

798 changes: 798 additions & 0 deletions _build/html/06. Linked List/143. Reorder List.html

Large diffs are not rendered by default.

775 changes: 775 additions & 0 deletions _build/html/06. Linked List/19. Remove Nth Node From End of List.html

Large diffs are not rendered by default.

831 changes: 831 additions & 0 deletions _build/html/06. Linked List/206. Reverse Linked List.html

Large diffs are not rendered by default.

755 changes: 755 additions & 0 deletions _build/html/06. Linked List/21. Merge Two Sorted Lists.html

Large diffs are not rendered by default.

783 changes: 783 additions & 0 deletions _build/html/06. Linked List/23. Merge k Sorted Lists.html

Large diffs are not rendered by default.

632 changes: 632 additions & 0 deletions _build/html/06. Linked List/README.html

Large diffs are not rendered by default.

727 changes: 727 additions & 0 deletions _build/html/07. Trees/100. Same Tree.html

Large diffs are not rendered by default.

767 changes: 767 additions & 0 deletions _build/html/07. Trees/102. Binary Tree Level Order Traversal.html

Large diffs are not rendered by default.

725 changes: 725 additions & 0 deletions _build/html/07. Trees/104. Maximum Depth of Binary Tree.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

759 changes: 759 additions & 0 deletions _build/html/07. Trees/124. Binary Tree Maximum Path Sum.html

Large diffs are not rendered by default.

749 changes: 749 additions & 0 deletions _build/html/07. Trees/199. Binary Tree Right Side View.html

Large diffs are not rendered by default.

719 changes: 719 additions & 0 deletions _build/html/07. Trees/226. Invert Binary Tree.html

Large diffs are not rendered by default.

729 changes: 729 additions & 0 deletions _build/html/07. Trees/230. Kth Smallest Element in a BST.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

839 changes: 839 additions & 0 deletions _build/html/07. Trees/297. Serialize and Deserialize Binary Tree.html

Large diffs are not rendered by default.

788 changes: 788 additions & 0 deletions _build/html/07. Trees/572. Subtree of Another Tree.html

Large diffs are not rendered by default.

740 changes: 740 additions & 0 deletions _build/html/07. Trees/98. Validate Binary Search Tree.html

Large diffs are not rendered by default.

647 changes: 647 additions & 0 deletions _build/html/07. Trees/README.html

Large diffs are not rendered by default.

792 changes: 792 additions & 0 deletions _build/html/08. Tries/208. Implement Trie (Prefix Tree).html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

836 changes: 836 additions & 0 deletions _build/html/08. Tries/212. Word Search II.html

Large diffs are not rendered by default.

623 changes: 623 additions & 0 deletions _build/html/08. Tries/README.html

Large diffs are not rendered by default.

763 changes: 763 additions & 0 deletions _build/html/09. Heap - Priority Queue/1046. Last Stone Weight.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

623 changes: 623 additions & 0 deletions _build/html/09. Heap - Priority Queue/README.html

Large diffs are not rendered by default.

773 changes: 773 additions & 0 deletions _build/html/10. Backtracking/39. Combination Sum.html

Large diffs are not rendered by default.

800 changes: 800 additions & 0 deletions _build/html/10. Backtracking/79. Word Search.html

Large diffs are not rendered by default.

620 changes: 620 additions & 0 deletions _build/html/10. Backtracking/README.html

Large diffs are not rendered by default.

811 changes: 811 additions & 0 deletions _build/html/11. Graphs/133. Clone Graph.html

Large diffs are not rendered by default.

749 changes: 749 additions & 0 deletions _build/html/11. Graphs/178. Graph Valid Tree.html

Large diffs are not rendered by default.

744 changes: 744 additions & 0 deletions _build/html/11. Graphs/200. Number of Islands.html

Large diffs are not rendered by default.

747 changes: 747 additions & 0 deletions _build/html/11. Graphs/207. Course Schedule.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

757 changes: 757 additions & 0 deletions _build/html/11. Graphs/417. Pacific Atlantic Water Flow.html

Large diffs are not rendered by default.

632 changes: 632 additions & 0 deletions _build/html/11. Graphs/README.html

Large diffs are not rendered by default.

756 changes: 756 additions & 0 deletions _build/html/12. Advanced Graphs/892. Alien Dictionary.html

Large diffs are not rendered by default.

617 changes: 617 additions & 0 deletions _build/html/12. Advanced Graphs/README.html

Large diffs are not rendered by default.

732 changes: 732 additions & 0 deletions _build/html/13. One-D Dynamic Programming/198. House Robber.html

Large diffs are not rendered by default.

765 changes: 765 additions & 0 deletions _build/html/13. One-D Dynamic Programming/213. House Robber II.html

Large diffs are not rendered by default.

717 changes: 717 additions & 0 deletions _build/html/13. One-D Dynamic Programming/70. Climbing Stairs.html

Large diffs are not rendered by default.

644 changes: 644 additions & 0 deletions _build/html/13. One-D Dynamic Programming/README.html

Large diffs are not rendered by default.

661 changes: 661 additions & 0 deletions _build/html/14. Two-D Dynamic Programming/README.html

Large diffs are not rendered by default.

661 changes: 661 additions & 0 deletions _build/html/15. Greedy/README.html

Large diffs are not rendered by default.

670 changes: 670 additions & 0 deletions _build/html/16. Intervals/README.html

Large diffs are not rendered by default.

664 changes: 664 additions & 0 deletions _build/html/17. Math Geometry/README.html

Large diffs are not rendered by default.

660 changes: 660 additions & 0 deletions _build/html/18. Bit Manipulation/README.html

Large diffs are not rendered by default.

1,151 changes: 1,151 additions & 0 deletions _build/html/Book.html

Large diffs are not rendered by default.

710 changes: 710 additions & 0 deletions _build/html/CODE_OF_CONDUCT.html

Large diffs are not rendered by default.

645 changes: 645 additions & 0 deletions _build/html/CONTRIBUTING.html

Large diffs are not rendered by default.

1,141 changes: 1,141 additions & 0 deletions _build/html/README.html

Large diffs are not rendered by default.

610 changes: 610 additions & 0 deletions _build/html/SECURITY.html

Large diffs are not rendered by default.

211 changes: 211 additions & 0 deletions _build/html/_sources/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 7a55649

Please sign in to comment.