My DSA Journey
Day 14 - Trees (BFS)
Category: Trees
π― PRIMARY PROBLEM
RequiredBinary Tree Level Order Traversal
π Goal: Master BFS with queue for level-by-level traversal
π‘ RELATED PROBLEMS
Same Pattern - PracticeBinary Tree Right Side View
π BFS variant - get last node of each level
Binary Tree Zigzag Level Order
π BFS with alternating direction
Minimum Depth of Binary Tree
π BFS to find shortest path to leaf
1 of 4 problems completed
π‘ Pro Tip: Start with the primary problem to understand the core pattern. Then practice the related problems to reinforce your learning. You can skip related problems if you're short on time, but solving them will help you master the pattern faster!
π Recently Solved Problems
Compress String (Run-Length Encoding)
Approach: Use a two-pointer approach (read pointer and write pointer) to count consecutive identical characters and replace the sequence with the character followed by the count.
2025-10-31
Optimized
N/ABrute Force
N/ASpace
N/AVowels and Consonants
Approach: Use a predefined set (HashSet in Java/C++, set in Python) of vowels for fast O(1) lookups during a single string traversal.
2025-10-30
Optimized
N/ABrute Force
N/ASpace
N/ALetter Coverage (Pangram Check)
Approach: Use a fixed-size boolean array (size 26) to mark the presence of each letter (a-z) encountered during a single pass.
2025-10-30
Optimized
N/ABrute Force
N/ASpace
N/ADigit String Check
Approach: Iterate through the string and use a built-in character method (e.g., isdigit() or Character.isDigit()) to check if every character is a numerical digit.
2025-10-30
Optimized
N/ABrute Force
N/ASpace
N/AOdd Even Index
Approach: Iterate through the string index and check the parity (i % 2) of the index to route the character to the odd or even result string.
2025-10-30
Optimized
N/ABrute Force
N/ASpace
N/ATriangle and Pyramid Patterns
Approach: Use the row index (i) to determine the number of characters or spaces to print in the inner loop.
2025-10-24
Optimized
O(NΒ²)Brute Force
O(NΒ²)Space
O(1)Half Diamond Pattern
Approach: Print the pattern in two halves: the increasing part (top) and the decreasing part (bottom).
2025-10-24
Optimized
O(NΒ²)Brute Force
O(NΒ²)Space
O(1)Filled and Hollow Rectangle
Approach: Use two nested loops (i for rows, j for columns). For Hollow, add a conditional check for boundaries.
2025-10-24
Optimized
O(N*M)Brute Force
O(N*M)Space
O(1)Submatrix Sum
Approach: Brute force sum for small N (O(N^2)) or 2D Prefix Sum for optimization (O(1) query).
2025-10-23
Optimized
O(NΒ²)Brute Force
O(NΒ²)Space
O(NΒ²)Lower Triangle Sum
Approach: Iterate through the matrix and only sum elements where the row index (i) is greater than or equal to the column index (j), i.e., i >= j.
2025-10-23
Optimized
N/ABrute Force
N/ASpace
N/AMatrix Row and Column Sums
Approach: Calculate Row Sums by streaming and Column Sums using an auxiliary 1D array.
2025-10-23
Optimized
O(N*M)Brute Force
O(N*M)Space
O(M)Natural Numbers Sum
Approach: Use Gauss's formula, $S = rac{N(N+1)}{2}$, to calculate the sum in constant time.
2025-10-27
Optimized
N/ABrute Force
N/ASpace
N/AFactorial
Approach: Use an iterative loop starting from 1 up to N, multiplying the result at each step. Must handle data type overflow for large N.
2025-10-27
Optimized
N/ABrute Force
N/ASpace
N/AArithmetic Progression Check
Approach: Calculate the difference between the first two elements. Then, iterate through the rest of the array, checking if the difference between all consecutive pairs is constant.
2025-10-27
Optimized
N/ABrute Force
N/ASpace
N/AArithmetic Operators
Approach: Read two numbers and a character operator. Use conditional logic (if/else or switch) to perform the correct operation.
2025-10-27
Optimized
N/ABrute Force
N/ASpace
N/ATriangle Validator
Approach: Check the triangle inequality theorem: the sum of the lengths of any two sides of a triangle must be greater than the length of the third side.
2025-10-27
Optimized
N/ABrute Force
N/ASpace
N/ANumber Distribution (Frequency Count)
Approach: Use a hash map to count the occurrences of each number. Then, iterate through the map to find the counts of required numbers.
2025-10-26
Optimized
N/ABrute Force
N/ASpace
N/AFind Duplicate Number in Array
Approach: Treat the array as a linked list where the index i points to the value nums[i]. A duplicate creates a cycle.
2025-10-26
Optimized
N/ABrute Force
N/ASpace
N/AOdd and Even Sum
Approach: Use a single linear pass to check each element's parity and accumulate separate sums for odd and even numbers.
2025-10-25
Optimized
N/ABrute Force
N/ASpace
N/AThe Missing Number
Approach: Use the arithmetic series formula (N*(N+1)/2) to find the expected sum, and subtract the actual sum of the array elements to find the missing number.
2025-10-25
Optimized
N/ABrute Force
N/ASpace
N/AMax Element in Array
Approach: Initialize a variable to the smallest possible value and iterate through the array, updating the maximum value encountered.
2025-10-25
Optimized
N/ABrute Force
N/ASpace
N/ASparse Arrays
Approach: Use a Hash Map to count input string frequencies in O(N), then lookup queries in O(1).
2025-10-24
Optimized
O(N + M)Brute Force
O(N * M)Space
O(N)Duplicate Integer
Approach: Use a HashSet/Unordered Set to track seen numbers.
2025-10-23
Optimized
O(n)Brute Force
O(nΒ²)Space
O(n)Two Sum
Approach: Use hash map to store complements for O(n) lookup
2025-10-23
Optimized
O(n)Brute Force
O(nΒ²)Space
O(n)