This package provides three classes for working with data structures in LeetCode problems
Project description
LeetCode Data Structures
This package provides three classes for working with data structures in LeetCode problems:
TreeNode
: used for solving binary tree problemsListNode
: used for solving linked list problemsNode
: used for solving n-ary tree problems For each of the three classes, the structure and attribute names are the same as in LeetCode.
Additionally, two helper methods are provided for each class:
to_array
: converts an instance of the class to an array representation, which can be used in LeetCode problems.
from_array
: constructs an instance of the class from an array representation.
The to_array and from_array methods are useful because LeetCode often represents data structures using arrays, and converting to and from this format can be time-consuming and error-prone.
Installation
To install the package, use pip:
pip install leetcode_data_structure
Usage
Here we use this package to solve Leetcode 100 same tree
:
import unittest
from data_structure import TreeNode
class Solution:
def isSameTree(self, p: TreeNode | None, q: TreeNode | None) -> bool:
if p is None and q is None:
return True
if p is None or q is None:
return False
if p.val != q.val:
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
def test(testObj: unittest.TestCase, p_arr: list[int], q_arr: list[int], expected: bool) -> None:
p = TreeNode.from_array(p_arr)
q = TreeNode.from_array(q_arr)
so = Solution()
actual = so.isSameTree(p, q)
testObj.assertEqual(actual, expected)
class TestClass(unittest.TestCase):
def test_1(self):
test(self, [1, 2, 3], [1, 2, 3], True)
def test_2(self):
test(self, [1, 2], [1, None, 2], False)
def test_3(self):
test(self, [1, 2, 1], [1, 1, 2], False)
if __name__ == "__main__":
unittest.main()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file leetcode_data_structure-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: leetcode_data_structure-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b64322d736ca8b5346870dc75ac7d458f7d122bfa9c27028556e9b7b03d958e |
|
MD5 | e2e316e5a03b51d3e9de6a57179cbe12 |
|
BLAKE2b-256 | c575a5dd9b7f1445e45ce0366a6b47dd94960cf2175a38b39c7628d7ba0261f2 |