Fading Coder

One Final Commit for the Last Sprint

Reconstructing a Binary Tree from Preorder and Inorder Traversal Arrays

The problem requires constructing a binary tree given two integer arrays, preorder and inorder, representing the tree's preorder and inorder traversals, respectively, and returning the root node. The solution leverages the distinct properties of these traversal orders. In a preorder traversal, nodes...

Computing the nth Term of the Fibonacci Sequence Using Iteration, Recursion, and Tail Recursion

The Fibonacci sequence begins with two initial values and each subsequent term equals the sum of the previous two. For this treatment, the sequence starts 1, 1, 2, 3, 5, 8, .... Iterative Approach An iterative method computes the desired term by updating two running values across a loop. long iter_f...

Recursive Algorithms for Combinatorial Enumeration and Tree Traversal

Recursive Implementation of Combinatorial Enumeration Given two integers n and m, generate all combinations of m distinct integers from the set {1, 2, ..., n} in lexicographical order. #include <iostream> #include <vector> using namespace std; vector<vector<int>> results; vec...

Mastering Recursion Through Three Classic Problems

Tower of Hanoi: A Recursive Challenge The Tower of Hanoi is a classic puzzle involving three pegs and a number of disks of different sizes. The objective is to move the entire stack to another peg, following these rules: only one disk can be moved at a time, and a larger disk cannot be placed on top...

C Programming Examples: Functions and Recursion

Score to Grade Conversion This program converts numerical scores to letter grades using a switch-case structure. #include <stdio.h> char calculate_grade(int value); int main() { int input; char result; while (scanf("%d", &input) != EOF) { result = calculate_grade(input); printf(&...

Recursive Algorithm Implementations in C++

Collatz Conjecture Verification The Collatz conjecture states that for any positive integer, if it's even divide by 2, if it's odd multiply by 3 and add 1, eventually the sequence will reach 1. This program calculates the number of steps required. #include <iostream> using namespace std; int c...

Adding Two Numbers Represented by Linked Lists

Problem Description Given two non-empty linked lists representing non-negative integres in reverse digit order, combine the numbers and return the result as a linked list. Basic Iterative Approach class Solution { public ListNode addTwoNumbers(ListNode first, ListNode second) { ListNode dummyHead =...

Backtracking Solutions for Combination Sum III and Letter Combinations of a Phone Number

Combination Sum III Find all valid combinations of k distinct numbers from 1 to 9 that sum to a target value n. A backtracking approach is used with two pruning strategies: Skip further exploration if the current sum exceeds n. Limit the loop range to ensure enough remaining numbers are available to...

Recursive Strategies for Binary Tree Manipulation and Summation

Inverting a Binary Tree To invert a binary tree, swap the left and right children of every node recursively. The core idea is: for each node, first swap its children, then recursively apply the same operation to both subtrees. The base case occurs when the current node is nullptr, in which case no a...

Implementing Core Computational Functions in C Programming

Task 1: Converting a Numerical Score to a Letter Grade The function grade_converter maps an integer score to a corresponding letter grade ('A' through 'E'). The functon takes an int parameter and returns a char value. A corrected implementation of a similar switch-case structure is shown below: char...