【leetcode】DFS递归题目总结

embedded/2024/10/18 2:41:23/

DFS(深度优先搜索) 深度优先搜索是一种用于遍历搜索树或图算法,其基本思路是从起始节点开始,沿着一条路径一直走到底,直到无法再走下去为止,然后回溯到上一个节点,继续走到另外一个路径,重复上述过程,直到遍历完所有节点。 

dfs框架:

void dfs(int cur, vector<int>& visited, vector<vector<int>>& graph) {visited[cur] = 1; // 标记当前节点已经被访问// 处理当前节点curfor (int i = 0; i < graph[cur].size(); i++) {int next = graph[cur][i];if (!visited[next]) { // 如果下一个节点未被访问dfs(next, visited, graph); // 继续访问下一个节点}}
}
void dfsTraversal(vector<vector<int>>& graph) {int n = graph.size();vector<int> visited(n, 0); // 初始化访问数组for (int i = 0; i < n; i++) {if (!visited[i]) { // 如果当前节点未被访问dfs(i, visited, graph); // 从当前节点开始进行深度优先遍历}}
}

94. 二叉树的中序遍历

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> res;void traversal(TreeNode* root) {if (root == nullptr) return;traversal(root->left);res.push_back(root->val);traversal(root->right);}vector<int> inorderTraversal(TreeNode* root) {traversal(root);return res;}
};

104. 二叉树的最大深度

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int maxDepth(TreeNode* root) {if (root == nullptr) return 0;return max(maxDepth(root->left), maxDepth(root->right)) + 1;}
};

226. 翻转二叉树

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:void travsal(TreeNode* root) {if (root == nullptr) return;TreeNode* temp = root->right;root->right = root->left;root->left = temp;travsal(root->left);travsal(root->right);}TreeNode* invertTree(TreeNode* root) {travsal(root);return root;}
};

101. 对称二叉树

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:bool dfs(TreeNode* left, TreeNode* right) {if (left == nullptr && right == nullptr) {return true;}if (left == nullptr || right == nullptr) {return false;}if (left->val != right->val) {return false;}return dfs(left->left, right->right) && dfs(left->right, right->left);}bool isSymmetric(TreeNode* root) {return dfs(root, root);}
};

543. 二叉树的直径

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int resDiameter = 0;int maxDepth(TreeNode* root) {if (root == nullptr) return 0;int maxLeft = maxDepth(root->left);int maxRight = maxDepth(root->right);int curDiameter = maxLeft + maxRight;resDiameter = max(curDiameter, resDiameter);return max(maxLeft, maxRight) + 1;}int diameterOfBinaryTree(TreeNode* root) {maxDepth(root);return resDiameter;}
};

200. 岛屿数量

class Solution {
public:void dfs(vector<vector<char>>& grid, int i, int j) {int m = grid.size();int n = grid[0].size(); if (i < 0 || j < 0 || i >= m || j >= n) {return;}if (grid[i][j] == '0') {return;}if (grid[i][j] == '1') {grid[i][j] = '0';dfs(grid, i, j - 1);dfs(grid, i, j + 1);dfs(grid, i - 1, j);dfs(grid, i + 1, j);}}int numIslands(vector<vector<char>>& grid) {int m = grid.size();int n = grid[0].size(); int res = 0;for (int i = 0; i < m; ++i) {for (int j = 0; j < n; ++j) {if (grid[i][j] == '1') {res += 1;dfs(grid, i, j);} }}return res;}
};

LCR 054. 把二叉搜索树转换为累加树

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int sum = 0;void travesal(TreeNode* root) {if (root == nullptr) return;travesal(root->right);sum += root->val;root->val = sum;travesal(root->left);}TreeNode* convertBST(TreeNode* root) {travesal(root);return root;}
};

98. 验证二叉搜索树

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:bool myIsValidBST(TreeNode* root, long long min, long long max) {if (root == nullptr) return true;if (root->val <= min || root->val >= max) return false;return myIsValidBST(root->left, min, root->val) && myIsValidBST(root->right, root->val, max);}bool isValidBST(TreeNode* root) {return myIsValidBST(root, LLONG_MIN, LLONG_MAX);}
};

700. 二叉搜索树中的搜索

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:TreeNode* searchBST(TreeNode* root, int val) {if (root == nullptr) return nullptr;if (root->val == val) return root;return root->val > val ? searchBST(root->left, val) : searchBST(root->right, val);}
};

199. 二叉树的右视图

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> res;void dfs(TreeNode* root, int depth) {if (root == nullptr) return;if (depth == res.size()) {res.push_back(root->val);}depth++;dfs(root->right, depth);dfs(root->left, depth);}vector<int> rightSideView(TreeNode* root) {dfs(root, 0);return res;}
};

236. 二叉树的最近公共祖先

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (!root) return NULL;if (root == p) return p;if (root == q) return q;TreeNode* left = lowestCommonAncestor(root->left, p, q);TreeNode* right = lowestCommonAncestor(root->right, p, q);if (left && right) return root;return left ? left : right; }
};

111. 二叉树的最小深度

class Solution {
public:int minDepth(TreeNode *root) {if (root == nullptr) {return 0;}if (root->left == nullptr && root->right == nullptr) {return 1;}int min_depth = INT_MAX;if (root->left != nullptr) {min_depth = min(minDepth(root->left), min_depth);}if (root->right != nullptr) {min_depth = min(minDepth(root->right), min_depth);}return min_depth + 1;}
};

104. 二叉树的最大深度

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int maxDepth(TreeNode* root) {if (root == nullptr) return 0;return max(maxDepth(root->left), maxDepth(root->right)) + 1;}
};

124. 二叉树中的最大路径和

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int maxSum = INT_MIN;int maxGain(TreeNode* root) {if (root == nullptr) return 0;int left = max(maxGain(root->left), 0);int right = max(maxGain(root->right), 0);int cur = root->val + left + right;maxSum = max(cur, maxSum);return root->val + max(left, right);}int maxPathSum(TreeNode* root) {maxGain(root);return maxSum;}
};

230. 二叉搜索树中第K小的元素

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int index = 0;int res = 0;void travesal(TreeNode* root, int k) {if (root == nullptr) return;travesal(root->left, k);if (++index == k) {res = root->val;return;}travesal(root->right, k);}int kthSmallest(TreeNode* root, int k) {travesal(root, k);return res;}
};

652. 寻找重复的子树

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:unordered_map<string, int> seen;vector<TreeNode*> res;string travesal(TreeNode* root) {if (root == nullptr) return "#";string left = travesal(root->left);string right = travesal(root->right);string subTree = left + "," + right + "," + to_string(root->val);auto it = seen.find(subTree);if (it != seen.end() && it->second == 1) {res.push_back(root);}seen[subTree]++;return subTree;}vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {travesal(root);return res;}
};


http://www.ppmy.cn/embedded/32993.html

相关文章

如何在 Ubuntu 服务器上使用 GlusterFS 创建冗余存储池

简介 冗余和高可用性对于各种服务器活动都是必要的。在数据存储方面存在单点故障对于任何关键数据来说都是非常危险的配置。 虽然许多数据库和其他软件允许您在单个应用程序的上下文中分散数据&#xff0c;但其他系统可以在文件系统级别操作&#xff0c;以确保数据在写入磁盘时…

设计模式——行为型模式——策略模式

策略模式 定义 策略模式定义了一系列算法&#xff0c;并将每个算法封装起来&#xff0c;使它们可以相互替换&#xff0c;且算法的变化不会影响使用算法的客户。 策略模式属于对象行为模式&#xff0c;它通过对算法进行封装&#xff0c;把使用算法的责任和算法的实现分割开来&a…

本地部署大模型ollama+docker+open WebUI/Lobe Chat

文章目录 大模型工具Ollama下载安装运行Spring Ai 代码测试加依赖配置写代码 ollama的web&Desktop搭建部署Open WebUI有两种方式Docker DesktopDocker部署Open WebUIDocker部署Lobe Chat可以配置OpenAI的key也可以配置ollama 大模型的选择 本篇基于windows环境下配置 大模型…

Leetcode—422. 有效的单词方块【简单】Plus

2024每日刷题&#xff08;126&#xff09; Leetcode—422. 有效的单词方块 实现代码 class Solution { public:bool validWordSquare(vector<string>& words) {int row words.size();for(int i 0; i < row; i) {// 当前这一行的列数int col words[i].length(…

我遇到的前端疑难杂症

1.使用vite打包后的页面打不开 问题&#xff1a;不要直接打开dist的index.html 解决方案&#xff1a;点击dist文件夹进入终端后输入npm run preview就能打开了 2.node下载包时下载不了最新版/下载错误的包 问题&#xff1a;没清缓存 解决方案&#xff1a;清除缓存&#xff08;…

mindjourney和stable diffusion该怎么选?

很多刚开始接触AI绘画的人来说&#xff0c;mindjourney和stable diffusion该怎么选&#xff1f; 坦白来说&#xff0c;这两种对于普通用户来说&#xff0c;门槛都会有一些。 mj的门槛在于需要梯子&#xff0c;而且要想持续出图&#xff0c;还需要付费&#xff0c;对以免费为乐…

上位机图像处理和嵌入式模块部署(树莓派4b读写json数据)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 前面我们说过&#xff0c;ini文件是用来进行配置的&#xff0c;数据库是用来进行数据存储的。那json是用来做什么的呢&#xff0c;json一般是用来做…

ubuntu20中ros与anaconda的python版本冲突问题

系统环境 原本系统是ubuntu20 noetic&#xff0c;python都在/usr/bin中&#xff0c;一共是两个版本的python&#xff0c;一个是python3.8&#xff0c;另一个是python2.7。 问题发现 当安装anaconda后&#xff0c;并且将anaconda的bin目录加入到系统环境中时候&#xff0c;…