Skip to content

GH-285: solve Leetcode 100 #308

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

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collections/leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<td>97</td>
<td>98</td>
<td>99</td>
<td>100</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode100'>100</a></td>
<tr>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode101'>101</a></td>
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode102'>102</a></td>
Expand Down
3 changes: 3 additions & 0 deletions collections/leetcode/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ problems:
- name: 88
languages: python
level: easy
- name: 100
languages: python
level: easy
- name: 101
languages: cpp
level: easy
Expand Down
3 changes: 3 additions & 0 deletions problems/leetcode100/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
solution
*.dSYM
python/main/__pycache__
10 changes: 10 additions & 0 deletions problems/leetcode100/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Leetcode Problem

## Usage

Test program

```
# Run all tests
python solution_test.py
```
2 changes: 2 additions & 0 deletions problems/leetcode100/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1,2,3
1,2,3
1 change: 1 addition & 0 deletions problems/leetcode100/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
2 changes: 2 additions & 0 deletions problems/leetcode100/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1,2
1,_,2
1 change: 1 addition & 0 deletions problems/leetcode100/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
2 changes: 2 additions & 0 deletions problems/leetcode100/data/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1,2,1
1,1,2
1 change: 1 addition & 0 deletions problems/leetcode100/data/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
10 changes: 10 additions & 0 deletions problems/leetcode100/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Leetcode 100. Same Tree",
"link": "https://leetcode.com/problems/same-tree/",
"tags": [
"Tree",
"Depth-First Search",
"Breath-First Search",
"Binary Tree"
]
}
20 changes: 20 additions & 0 deletions problems/leetcode100/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition for a binary tree node.
from typing import Optional


class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
if not q or not p:
return False
if p.val!= q.val:
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
58 changes: 58 additions & 0 deletions problems/leetcode100/python/tests/solution_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import sys
import unittest
from os import listdir
from os.path import abspath, dirname, join

python_source = dirname(dirname(abspath(__file__)))
sys.path.append(python_source)
from main.solution import Solution, TreeNode

test_data_directory = join(dirname(python_source), 'data')
test_cases = set([data_file.split('.')[0] for data_file in listdir(test_data_directory)])

def read_input(input_file):
with open(input_file, 'r') as f:
nums1 = [int(num) if num != '_' else None for num in f.readline().strip().strip('][').split(',') ]
nums2 = [int(num) if num != '_' else None for num in f.readline().strip().strip('][').split(',') ]
return nums1, nums2

def read_output(output_file):
with open(output_file, 'r') as f:
k = True if f.readline().strip() == 'true' else False
return k

class TestSequenceMeta(type):
def __new__(mcs, name, bases, dict):

def list_to_tree(nums):
if not nums:
return None
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = list_to_tree(nums[:mid])
root.right = list_to_tree(nums[mid+1:])
return root

def gen_test(input_file, output_file):
def test(self):
solution = Solution()
nums1, nums2 = read_input(input_file)
first_tree = list_to_tree(nums=nums1)
second_tree = list_to_tree(nums=nums2)
expected_k = read_output(output_file)
actual_k = solution.isSameTree(first_tree, second_tree)
self.assertEqual(actual_k, expected_k)
return test

for test_case in test_cases:
test_name = f'test_{test_case}'
input_file = join(test_data_directory, f'{test_case}.in')
output_file = join(test_data_directory, f'{test_case}.out')
dict[test_name] = gen_test(input_file, output_file)
return type.__new__(mcs, name, bases, dict)

class TestSequence(unittest.TestCase, metaclass=TestSequenceMeta):
pass

if __name__ == '__main__':
unittest.main()