Leetcode

This is an overview of LeetCode problem-solving tutorials, currently containing 82 problems. Each tutorial follows the same structure: the solution approach (explaining the rationale), algorithm steps, complexity analysis, boundary cases and common mistakes, and finally, a comparison table of three solutions. For practice by problem type (arrays, hash tables, dynamic programming, graph theory, etc.), you can find the problem type index in the "How to Solve LeetCode in the AI Era" tutorial.

Implement Trie II Problem Solving Tutorial Diagram | LeetCode 1804

Implement Trie II Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 1804 Best Practice

A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

Implement the Trie class:

Trie() Initializes the trie object.
void insert(String word) Inserts the string word into the trie.
int countWordsEqualTo(String word) Returns the number of instances of the string word in the trie.
int countWordsStartingWith(String prefix) Returns the number of strings in the trie that have the string prefix as a prefix.
void erase(String word) Erases the string word from the trie.

Implement Trie II Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 1804 Best Practice Read More »

Leetcode, Technical Article, , , ,
Design Add and Search Words Data Structure Problem Solving Tutorial Diagram | LeetCode 211

Design Add and Search Words Data Structure Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 211 Best Practice

Design a data structure that supports adding new words and finding if a string matches any previously added string.

Implement the WordDictionary class:

WordDictionary() Initializes the object.
void addWord(word) Adds word to the data structure, it can be matched later.
bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.

Design Add and Search Words Data Structure Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 211 Best Practice Read More »

Leetcode, Technical Article, , , ,
Implement Trie Problem Solving Tutorial Diagram | LeetCode 208

Implement Trie Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 208 Best Practice

A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

Implement the Trie class:

Trie() Initializes the trie object.
void insert(String word) Inserts the string word into the trie.
boolean search(String word) Returns true if the string word is in the trie (ie, was inserted before), and false otherwise.
boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.

Implement Trie Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 208 Best Practice Read More »

Technical Article, Leetcode, , , ,
Lowest Common Ancestor of a Binary Search Tree: Problem Solving Tutorial Diagram | LeetCode 235

Lowest Common Ancestor of a Binary Search Tree: Solution Tutorial - Complete Comparison of 3 Solutions | LeetCode 235 Best Practice

Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Lowest Common Ancestor of a Binary Search Tree: Solution Tutorial - Complete Comparison of 3 Solutions | LeetCode 235 Best Practice Read More »

Leetcode, Technical Article, , , ,
Kth Smallest Element in a BST: Solution Tutorial Diagram | LeetCode 230

Kth Smallest Element in a BST: A Complete Comparison of 3 Solutions | LeetCode 230 Best Practice

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Kth Smallest Element in a BST: A Complete Comparison of 3 Solutions | LeetCode 230 Best Practice Read More »

Leetcode, Technical Article, , , , ,
A diagram illustrating the solution to the Validate Binary Search Tree problem | LeetCode 98

Validate Binary Search Tree Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 98 Best Practice

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.

Validate Binary Search Tree Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 98 Best Practice Read More »

Leetcode, Technical Article, , , ,
A diagram illustrating the solution to the problem "Construct a Binary Tree from Preorder" | LeetCode 105

Constructing a Binary Tree from Preorder: A Complete Comparison of 3 Solutions | LeetCode 105 Best Practice

Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

Constructing a Binary Tree from Preorder: A Complete Comparison of 3 Solutions | LeetCode 105 Best Practice Read More »

Leetcode, Technical Article, , , ,
A diagram illustrating the solution to the problem "Subtree of Another Tree" | LeetCode 572

Subtree of Another Tree Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 572 Best Practice

Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.

A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.

Subtree of Another Tree Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 572 Best Practice Read More »

Leetcode, Technical Article, , , ,
Serialize and Deserialize Binary Tree Problem Solving Tutorial Diagram | LeetCode 297

Tutorial on solving Serialize and Deserialize Binary Tree problems: A complete comparison of 3 solutions | LeetCode 297 Best Practice

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Tutorial on solving Serialize and Deserialize Binary Tree problems: A complete comparison of 3 solutions | LeetCode 297 Best Practice Read More »

Leetcode, Technical Article, , , ,
A diagram illustrating the solution to the Binary Tree Level Order Traversal problem | LeetCode 102

Binary Tree Level Order Traversal Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 102 Best Practice

Given the root of a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

Binary Tree Level Order Traversal Problem Solving Tutorial: A Complete Comparison of 3 Solutions | LeetCode 102 Best Practice Read More »

Leetcode, Technical Article, , , ,