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

Connected Components, Team Formation, and Zero-Sum Subarray Algorithms

Identify isolated groups of zeros in a grid that do not border grid edges. Traverse connected components using BFS while tracking boundary collisions. #include <vector> #include <queue> #include <iostream> int main() { int rows, cols; std::cin >> cols >> rows; std::vector grid(rows, std::vector&lt ...

Posted on Fri, 11 Sep 2026 16:10:40 +0000 by mcdsoftware

CSP-S 2022 Solutions

The problems in this set require careful handling of edge cases and efficient algorithms. Below are the solutions for all four tasks. T1: Holiday Plan Given constraints n ≤ 2.5 × 103, an O(n2) approach is feasible. We enumerate the middle two vertices B and C. For a valid pair (B, C), we pre‑compute the set of possible A (from B) and possible D ...

Posted on Thu, 10 Sep 2026 16:44:49 +0000 by dscapuano

Solving the Pushing Boxes Problem with Single Priority Queue BFS

The UVA589 problem requires finding the optimal path to push a box to a target location. The optimization criteria have two levels: primarily minimizing the number of pushes, and secondarily minimizing the total number of moves when push counts are equal. Key Problem Constraints The primary objective is to minimize push operations, not walki ...

Posted on Mon, 24 Aug 2026 16:45:15 +0000 by seodevhead

Floyd Algorithm for Graph Shortest Path and BFS with A* for Knight Moves

Floyd Algorithm for All-Pairs Shortest Path Implement Floyd's algorithm to compute shortest paths between all pairs of vertices in an undirected weighted graph. The graph is defined by vertices labeled from 1 to n and m edges with positive weights. For multiple queries, output the shotrest distance betwean two vertices or -1 if no path exists. ...

Posted on Sun, 23 Aug 2026 16:52:46 +0000 by Warmach

Competitive Programming Problem Solutions: BFS, String Manipulation, and Mathematical Logic

This problem involves a BFS simulation on an ice floor grid. The movement mechanics require sliding in a chosen direction until hitting an obstacle. The algorithm explores four directions from each position, continuing to slide until a wall is encountered, at which point the stopping position becomes a new node in the traversal. Key implementat ...

Posted on Thu, 20 Aug 2026 16:34:01 +0000 by jj33

ABC311 Contest Solutions

A - First ABC Solution We can track the first appearence of each character using boolean flags. By iterating through the string, we can determine the earliest position where all three required characters have been encountered. #include <iostream> #include <string> using namespace std; int main() { int length; string input; ...

Posted on Tue, 18 Aug 2026 16:36:57 +0000 by sysop

Essential Algorithms for Programming Competition Preparation

This collection presents fundamental algorithms and their applications to simple problems, primari sourced from the Lanqiao Cup competition. The problems are relatively straightforward, focusing more on algorithm templates and basic approaches. For better algorithm retention, the implementations are concise, frequently utilizing built-in C++ fu ...

Posted on Wed, 29 Jul 2026 16:32:20 +0000 by ThaboTheWuff

Zigzag Level Order Traversal of Binary Tree

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as: [ [3], [20,9], [15,7] ] Appr ...

Posted on Mon, 27 Jul 2026 16:05:55 +0000 by Hypnos

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