Essential Binary-Tree Algorithms and Their Implementations
In-Order Traversal
Recursive
List<Integer> inorder(TreeNode node) {
List<Integer> out = new ArrayList<>();
walk(node, out);
return out;
}
void walk(TreeNode cur, List<Integer> acc) {
if (cur == null) return;
walk(cur.left, acc);
acc.add(cur.val);
walk(cur.right, acc);
}
Iterative (Single Sta ...
Posted on Mon, 21 Sep 2026 16:25:53 +0000 by otterbield
The Confounding Switches - Solution
The Confounding Switches
Time Limit: C/C++ 1000MS, Other Languages 2000MS
Memory Limit: C/C++ 256MB, Other Languages 512MB
Description
Have you ever played the "Light Switch" game?
There are \(25\) lights arranged in a \(5×5\) grid.
Each light has a switch that can toggle its state.
In each move, a player can toggle one light's sta ...
Posted on Fri, 18 Sep 2026 16:14:26 +0000 by zachatk1
Minimal Coprime Groups Partitioning via Depth-First Search
Suppose you are given an integer array arr. The goal is to split it into the fewest possible subsets such that every pair of elements inside the same subset is coprime (their greatest common divisor equals 1).
We can solve this problem using a DFS backtracking approach. Below are two distinct strategies, each corresponding to a different way of ...
Posted on Tue, 08 Sep 2026 16:30:37 +0000 by PhilVaz
Recursive Techniques for Generating Subsets and Permutations
Recursion can be categorized into path-aware and path-unaware forms. Most tree-related recursions are path-aware. Fundamentally, recursion implements depth-first search (DFS). To solve problems recursively, treat the recursive function as a black box that handles a subproblem, then reuse it. Beginners often try to fully expand the recursion, wh ...
Posted on Sat, 05 Sep 2026 16:48:55 +0000 by denoteone
Word Chain Problem from NOIP2000 Advanced Group
The word chain problem involves constructing the longest possilbe sequence ("dragon") from a given set of words, starting with a specified character. Each word may be used at most twice in the chain. When two words are joined, overlapping parts are merged into one—e.g., beast and astonish form beastonish. Importantly, no word in the c ...
Posted on Mon, 17 Aug 2026 16:03:01 +0000 by ryankentp
Backtracking Algorithm Practice: Combination Sum III and Letter Combinations
Problem 216: Combination Sum III
Description: Given two integers k and n, find all possible combinations of k numbers from 1 to 9 that add up to n. Each number can only be used once in a combination.
Approach
This problem requires finding subsets of size k from the set [1,2,3,4,5,6,7,8,9] where the sum equals n. The parameter k represents the d ...
Posted on Sat, 15 Aug 2026 16:09:44 +0000 by heerajee
Tree Problem Summary
Introduction
During the summer vacation, I systematically studied various operations on trees through Teacher Tuo's sharing and discovered many useful techniques, which I now summarize.
Teacher Tuo is amazing!
One
Problem Type: Batch processing queries of the form f(dep(lca(x,y))), where f(x) is a function of x.
Approach: First perform tree ...
Posted on Sat, 01 Aug 2026 16:21:20 +0000 by D_tunisia
Graph Algorithms and Critical Path Analysis in C
Depth-First Search on Adjacency-Matrix Graphs
void DFS(MGraph G, Vertex v, void (*visit)(Vertex)) {
visit(v);
Visited[v] = true;
for (Vertex w = 0; w < G->Nv; ++w)
if (G->G[v][w] && !Visited[w])
DFS(G, w, visit);
}
Breadth-First Search on Adjacency-List Graphs
void BFS(LGraph G, Vertex s, void ...
Posted on Wed, 22 Jul 2026 16:36:11 +0000 by han2754
Determining Graph Connectivity using DFS and BFS
Algorithmic Approach
When handling graph problems, especially those involving large datasets, an adjacency list is often preferred over an adjacency matrix to optimize memory usage, particularly when the vertex count may exceed standard limits. To determine if an undirected graph is connected, one can traverse the structure using either Depth-F ...
Posted on Sun, 19 Jul 2026 16:48:56 +0000 by Jay87
Solutions for 2024 RoboCom CAIP Programming Skills Provincial Competition
RC-u1 Heat Wave
Problem Summary: Given daily maximum temperatures and the day of the week for the first day, count how many days have temperatures ≥ 35°C. Days falling on weekends (Saturday and Sunday) should be counted separately.
Solution: Iterate through the temperature data while tracking the current weekday. For each temperature ≥ 35, incr ...
Posted on Tue, 07 Jul 2026 17:58:05 +0000 by [UW] Jake