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...
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...
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(&...
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...
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 =...
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...
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...
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...
Define the TreeView control in XAML with a HierarchicalDataTemplate to handle the nested structure: <TreeView x:Name="CategoryTreeView" Height="400" Width="400"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Children}"> <TextBlock Text="{Binding Name}"...
Every recursive tree traversal follows a consistent decomposition built from three parts: a function signature that accepts the current subtree and an output collector, a base case that stops descent, and the logic that treats each deeper call as already completed. Recursive Depth-First Traversal Th...