Leetcode No.78 子集_公众号:算法攻城狮-CSDN博客
Leetcode No.90 子集 II(DFS)_公众号:算法攻城狮-CSDN博客
int n, m;//n:有几个数 m:要几个
bool used[ ];//是否用过
int ans[ ];//答案
void dfs(int u){
if (终止条件){//到头就走
存放结果
return ;//退出
}
//选择:本层集合中元素(树中节点孩子的数量就是集合的大小
for (int i = 开始的地方; i <= n; i++)//枚举每一种情况
if (used[i] == 0) {//判断是否用过
加入结果 设为用过
dfs(u + 1);//下一个数字
回溯:回到没用过
}
return ;//退出
}
int main(){
dfs(1);//开始搜索,从1开始
return 0;
}
LeetCode No.17 电话号码的字母组合_公众号:算法攻城狮-CSDN博客
Leetcode No.39 组合总和_公众号:算法攻城狮-CSDN博客
Leetcode No.40 组合总和 II_公众号:算法攻城狮-CSDN博客
Leetcode No.46 全排列_公众号:算法攻城狮-CSDN博客
Leetcode No.47 全排列 II_公众号:算法攻城狮-CSDN博客
Leetcode No.51 N皇后_公众号:算法攻城狮-CSDN博客
Leetcode No.52 N皇后 II_公众号:算法攻城狮-CSDN博客
Leetcode No.60 排列序列_公众号:算法攻城狮-CSDN博客
Leetcode No.77 组合_公众号:算法攻城狮-CSDN博客
Leetcode No.79 单词搜索_公众号:算法攻城狮-CSDN博客
Leetcode No.89 格雷编码(DFS)_公众号:算法攻城狮-CSDN博客
Leetcode No.93 复原 IP 地址(DFS)_公众号:算法攻城狮-CSDN博客
Leetcode No.130 被围绕的区域(DFS)_公众号:算法攻城狮-CSDN博客
Leetcode No.131 分割回文串_公众号:算法攻城狮-CSDN博客
Leetcode No.133 克隆图(DFS)_公众号:算法攻城狮-CSDN博客
Leetcode No.200 岛屿数量(DFS)_公众号:算法攻城狮-CSDN博客
Leetcode No.216 组合总和 III_公众号:算法攻城狮-CSDN博客
Leetcode No.140 单词拆分 II(DFS)_公众号:算法攻城狮-CSDN博客
void dfs(TreeNode* root,vector<int> nums,vector<vector<int>>& rs){
if(root == nullptr){
return;
}
if(root->left==nullptr&&root->right==nullptr){
nums.push_back(root->val);
rs.push_back(nums);
return;
}
nums.push_back(root->val);
dfs(root->left,nums,rs);
dfs(root->right,nums,rs);
}
Leetcode No.94 二叉树的中序遍历_公众号:算法攻城狮-CSDN博客
Leetcode No.98 验证二叉搜索树_公众号:算法攻城狮-CSDN博客
Leetcode No.100 相同的树_公众号:算法攻城狮-CSDN博客
Leetcode No.101 对称二叉树_公众号:算法攻城狮-CSDN博客
Leetcode No.104 二叉树的最大深度_公众号:算法攻城狮-CSDN博客
Leetcode No.110 平衡二叉树_公众号:算法攻城狮-CSDN博客
Leetcode No.111 二叉树的最小深度_公众号:算法攻城狮-CSDN博客
Leetcode No.114 二叉树展开为链表_公众号:算法攻城狮-CSDN博客
Leetcode No.112 路径总和_公众号:算法攻城狮-CSDN博客
Leetcode No.113 路径总和 II_公众号:算法攻城狮-CSDN博客
Leetcode No.124 二叉树中的最大路径和_公众号:算法攻城狮-CSDN博客
Leetcode No.129 求根节点到叶节点数字之和_公众号:算法攻城狮-CSDN博客
Leetcode No.144 二叉树的前序遍历_公众号:算法攻城狮-CSDN博客
Leetcode No.145 二叉树的后序遍历_公众号:算法攻城狮-CSDN博客
Leetcode No.173 二叉搜索树迭代器(DFS)_公众号:算法攻城狮-CSDN博客
Leetcode No.226 翻转二叉树_公众号:算法攻城狮-CSDN博客
Leetcode No.257 二叉树的所有路径_公众号:算法攻城狮-CSDN博客
void bfs(TreeNode root) {
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll(); // Java 的 pop 写作 poll()
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}