Posts

Showing posts from August, 2026

Leetcode 78 : Subsets

Subsets on the Stack Recursion · Backtracking · The call stack Subsets on the Stack Every subset of a set is one path through a tree of yes/no decisions. Watch the machine walk that tree — pushing a frame each time it goes deeper, popping one each time it comes back. Input set { 1, 2, 3 } → expect 2³ = 8 subsets The machine Step through it The tree shows where you are. The stack shows how you got there — one frame per unfinished call, each one paused mid-line, waiting for the call above it to finish. start Press Play, or step with the arrow keys. Code · current line 1 void findSubsets( int index) { 2 if (index == nums.length) { 3 results.add( new ArrayList<>(current)); 4 return ; 5 } 6 current.add(nums[index]); // TAKE 7 findSubsets(index + 1); // explore 8 current.remove(current.size()-1); // UN-TA...