leetcode-trees-rs

Crates.ioleetcode-trees-rs
lib.rsleetcode-trees-rs
version0.1.149
sourcesrc
created_at2024-03-20 17:51:30.788699
updated_at2024-05-15 20:08:45.1135
descriptionA Rust library for Tree Node LeetCode problems.
homepagehttps://github.com/1kill2steal/leetcode-trees-rs
repositoryhttps://github.com/1kill2steal/leetcode-trees-rs
max_upload_size
id1180814
size41,546
1K2S (1Kill2Steal)

documentation

README

LeetCode Trees in Rust

Build IconDocs IconVersion IconLicense Icon

Description

This library is made to make any LeetCoders using Rust have a better experience at solving their LeetCode (LC) problems. It uses cargo make to have reproducible sub-modules (check leetcode-trees-rs/solutions/README.md) as well as implementing the definition of the binary trees on LC.


Quick start on using the library:

For TreeNode values (Binary Trees)

use leetcode_trees_rs::{
    prelude::*,
    utils::{symmetric_tree, tree, TreeNode},
};

struct Solution {} // Assigning an empty struct to make a solution impl block.

use std::{cell::RefCell, rc::Rc};
impl Solution {
    pub fn your_leetcode_fn() {}
}

#[cfg(test)]
mod tests {
    #[test]
    fn tests() {
        // . . .
    }
}

fn main() -> Result<()> {
    // Equivalent of:
    //        1
    //    2       2
    //  3   3   3   3
    let symmetric_tree_node = symmetric_tree!(1, 2, 3);

    // Equivalent of:
    //                   1
    //         2                   #
    //    3         3         #         #
    // 4     #   #     #   #     #   #     #
    let custom_tree = tree!(&[
        vec![Some(1)],
        vec![Some(2), None],
        vec![Some(3), Some(3)],
        vec![Some(4)],
    ]);

    // If you want trees that only branch to the left or to the right then
    // there're also the `left_tree!()` and `right_tree!()` macros!
    // Those macros can help you write your test runs easier.

    Ok(())
}

For ListNode values (Singly Linked Lists)

use leetcode_trees_rs::{list_node, prelude::*, utils::ListNode};

struct Solution {} // Assigning an empty struct to make a solution impl block.

use std::{cell::RefCell, rc::Rc};
impl Solution {
    pub fn your_leetcode_fn() {}
}

#[cfg(test)]
mod tests {
    #[test]
    fn tests() {
        // . . .
    }
}

fn main() -> Result<()> {
    // This is the very cumbersome manual way of writing your ListNode structs.
    let some_list = ListNode {
        val: 1,
        next: Some(Box::new(ListNode {
            val: 2,
            next: Some(Box::new(ListNode::new(3))),
        })),
    };
    // And this is the easier way:
    let another_list = list_node!(1, 2, 3);

    assert_eq!(some_list, another_list);

    Ok(())
}

LICENSE

The project is licensed under the MIT license.

Extra notes


Additional code templating can be found in This template file.

It's located at: solutions/lc0_general_nodes_template/src/main.rs


Additional documentation can be found in docs.rs.

Commit count: 60

cargo fmt