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

Solving the Generalized N-Sum Problem

Problem Statement For an input array and target value, return all distinct n-element tuples where the sum equals the target. The solution must avoid duplicate combinations in the result. Example: Input: [1, 0, -1, 0, -2, 2], target = 0, n = 4 Output: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]] Generalized Solution The approach uses recurs ...

Posted on Thu, 17 Sep 2026 16:36:34 +0000 by Pinkmischief

Finding the Bottom-Left Value in a Binary Tree

Problem Overview Continuing with binary tree traversal problems. This is problem 513: Find the value of the bottom-left node in a binary tree. Problem Statement Given the root of a binary tree, return the value of the bottommost-leftmost node in the tree. The tree has at least one node. Example 1: Input: root = [2, 1, 3] Output: 1 Example 2: I ...

Posted on Fri, 11 Sep 2026 16:22:58 +0000 by robcrozier

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

Recursive Solutions for Singly Linked List Operations

Understanding Recursion Recursion occurs when a procedure or function includes a call to itself. This is known as direct recursion. When function A calls function B, and function B then calls function A, this is called indirect recursion. Designing Recursive Algorithms Recursive problem-solving follows a consistent pattern: decompose the entire ...

Posted on Mon, 31 Aug 2026 16:05:33 +0000 by Tarsonis21

Binary Search Tree Operations: Lowest Common Ancestor, Insertion, and Deletion

Lowest Common Ancestor in a BST (LeetCode 235) Given a BST and two nodes p and q, find their lowest common ancestor (LCA). The BST property allows an efficient traversal from root down: the LCA is the first node whose value lies between p->val and q->val (inclusive). Because the tree is ordered, moving left/right narrows down the range un ...

Posted on Wed, 26 Aug 2026 16:29:53 +0000 by Bogart

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

Recursive Tree Structure Implementation in C#

Hierarchical data structures are essential for building navigation menus, organization charts, and category trees. This article explores several C# implementations to transform flat data lists into recursive tree formats, suitable for JSON APIs, object-oriented models, and UI dropdowns. 1. Generating Recursive JSON Strings In some legacy system ...

Posted on Thu, 13 Aug 2026 16:06:44 +0000 by slimjim

Maximum Depth of Binary Trees: Recursive and Iterative Approaches

Maximum Depth of a Binary Tree The maximum depth of a binary tree is defined as the number of nodes along the longest path from the root node down to the farthest leaf node. A leaf node is a node that has no children. This problem can be solved using either a recursive depth-first search approach or an iterative breadth-first search approach. R ...

Posted on Thu, 13 Aug 2026 16:02:47 +0000 by Sj0wKOoMel

10 Fun and Simple Python Programs for Creative Exploration

Python is a versatile and beginner-friendly programming language that allows developers to express ideas creatively and efficiently. Below are ten engaging Python scripts that demonstrate the language's potential for visual creativity and interactive exploration using the turtle graphics module. Dynamic Color Spiral import turtle import ran ...

Posted on Wed, 12 Aug 2026 16:15:19 +0000 by Laogeodritt