Skip to main content

LeetCode Data Structures

This package provides three classes for working with data structures in LeetCode problems:

  • TreeNode: used for solving binary tree problems
  • ListNode: used for solving linked list problems
  • Node: 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()

Metadata

Release files for leetcode-data-structure 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for leetcode-data-structure 0.1.2
File Interpreter ABI Platform
leetcode_data_structure-0.1.2-py3-none-any.whl Python 3 none any Details

Release files / leetcode_data_structure-0.1.2-py3-none-any.whl

Download URL leetcode_data_structure-0.1.2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b64322d736ca8b5346870dc75ac7d458f7d122bfa9c27028556e9b7b03d958e
BLAKE2b-256 checksum
How to use checksums
c575a5dd9b7f1445e45ce0366a6b47dd94960cf2175a38b39c7628d7ba0261f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Security for leetcode-data-structure 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page