Generating Permutations and Combinations Using Depth-First Search

Permutations This article demonstrates a method for generating all permutations of a set of numbers using depth-first search (DFS). #include <iostream> #include <vector> #include <algorithm> using namespace std; const int MAX_SIZE = 100010; int size, sequence[MAX_SIZE]; bool visited[MAX_SIZE]; void generatePermutations(int ...

Posted on Tue, 04 Aug 2026 17:03:36 +0000 by nominator

Implementing Recursive Binary Tree Traversals: Preorder, Inorder, and Postorder

Constructing recursive tree traversal algorithms follows a standardized three-phase design pattern. First, establish the function signature by defining the node input and the container that will store traversal results. Second, define the termination condition to halt recursion when a leaf boundary is reached, usually by validating against a nu ...

Posted on Mon, 03 Aug 2026 16:48:43 +0000 by acirilo

Understanding Recursion Termination Conditions for Binary Tree Path Problems

LeetCode 257: Binary Tree All Paths Termination Condition Considerations The statement if (root == nullptr) return; serves multiple critical purposes in recursive tree algorithms: Primary Function Guard When placed in the main function provided by LeetCode, this check handles the empty tree case. If an empty tree is passed to the main function, ...

Posted on Sun, 12 Jul 2026 16:49:45 +0000 by knickerlas

Mastering Graph Search: DFS and BFS Strategies in Competitive Programming

Understanding Search Paradigms When approaching algorithmic challenges involving traversal, two primary methods dominate: Depth-First Search (DFS) and Breadth-First Search (BFS). While both traverse nodes in a graph or tree, their utility differs based on the problem constraints. BFS is fundamentally tied to the concept of shortest paths in unw ...

Posted on Thu, 09 Jul 2026 17:24:30 +0000 by studot

Graph Algorithms for Island Problems in Go

Water Flow Simulation Siumlate water flow using two visited matriecs for tracking. Depth-First Search Implementation package main import "fmt" var dirs = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} func main() { var rows, cols int fmt.Scanln(&rows, &cols) grid := make([][]int, rows) vis1 := make([][]bool, ...

Posted on Thu, 09 Jul 2026 16:35:05 +0000 by Pryach

NOIP Simulation Contest - Problem Solutions and Reflections

Overview This contest proved challenging despite seemingly moderate difficulty. The overall rating leans toward green to purple, but the execution was frustrating. T1 cost me significant points due to rushing through it—225 dropped to 175 points. Strategic lesson: even when T1 appears simple, allocating proper time (up to 1.5 hours is reasonabl ...

Posted on Wed, 08 Jul 2026 17:41:04 +0000 by x01440

Three LeetCode Problems: Binary Tree Split, Array Reduction, and Jump Game

Maximum Product of Splitted Binary Tree Given a binary tree with root node, remove exactly one edge to split the tree into two separate subtrees. The goal is to maximize the product of the sums of both resulting subtrees. Return the result modulo 10^9 + 7. Approach The key insight is that during a depth-first search that calculates subtree sums ...

Posted on Mon, 29 Jun 2026 16:28:43 +0000 by sapoxgn

Optimizing Binary Tree Diameter Calculation with Recursive Depth Analysis

The objective is to compute the diameter of a given binary tree. In this context, the diameter is defined as the length of the longest path between any two nodes within the structure. This path does not necessarily need to pass through the root node. The length of a path is quantified by the number of edges connecting the nodes. Algorithmic Str ...

Posted on Sun, 21 Jun 2026 17:14:22 +0000 by bobob

Backtracking Algorithms: A Comprehensive Introduction

Core Concept Backtracking is a systematic search technique that explores all possible solutions by building candidates incrementally and abandoning ("backtracking") a candidate as soon as it determines that the candidate cannot possibly lead to a valid solution. Problems Addressed Backtracking effectively solves the following categori ...

Posted on Sat, 30 May 2026 20:01:14 +0000 by kuri7548

Algorithm Solutions: Path Search, String Ranking, and Knapsack Problems

D - Path Traversal A straightforward depth-first search approach can solve this traversal problem. int nodes, edges, max_steps, min_cost, max_cost; vector<int> valid_endpoints; vector<pair<int, int>> graph[MAX_NODES]; void traverse(int current_node, int current_cost, int steps_taken) { if (current_cost > max_cost) retu ...

Posted on Fri, 29 May 2026 19:58:58 +0000 by volant