[Python] How do we access code after recursive call [closed]

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am studying leetcode questions for an upcoming interview and I dont understand a part of the backtracking algorithm. Specifically I don't understand how the code after the recursive call "backtracking(i+1) will be accessed by Python. This code is supposed to find all possible subsets of an array of numbers, for example the input [1,2,3] would result in [1], [2], [3], [1,2] ,[1,3], [2,] (not sure if I got all of them).

    the backtracking code that I'm looking at on a tutorial is this


    class Solution:
    def subsets (self, nums: List[int])-> List[List[int]]:
    n= len(nums)
    res, sol = [], []


    def backtracking:
    if i == n
    res.append(sol[:])
    return

    #dont pick nums
    backtrack(i+1)

    #pick nums
    sol.append(nums)
    backtrack(i+1)
    sol.pop();

    backtrack(0)
    return res



    I dont understand how the compiler reaches the code after the first backtrack(i+1). Wont that call keep the code in a constant loop that either sends us back to the method if i<n or returns us to backtrack(0) if i ==n?

    Continue reading...

Compartilhe esta Página