alt

二叉树的思路 先dfs 再回溯


public class Solution {
    ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
          int len = S.length;
          if (len == 0 || S == null){
              return  res;
          }
          Deque<Integer> path = new ArrayDeque<>();
          dfsOfSubSet(S,0,path);
          return  res;
    }

    public void dfsOfSubSet(int[] nums,int start,Deque<Integer> path){
          ArrayList<Integer> list = new ArrayList<>(path);
             res.add(list);
             int len = nums.length;
             if (start>= len){
                 return;
             }
        for (int i = start; i <len ; i++) {
              int num = nums[i];
              path.addLast(num);
              dfsOfSubSet(nums,i+1,path);
              path.removeLast();
        }
    }
}