mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-04-14 02:20:07 +08:00
Update
This commit is contained in:
242
README.md
242
README.md
@@ -152,246 +152,7 @@
|
||||
|
||||
# 算法模板
|
||||
|
||||
## 二分查找法
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
int searchInsert(vector<int>& nums, int target) {
|
||||
int n = nums.size();
|
||||
int left = 0;
|
||||
int right = n; // 我们定义target在左闭右开的区间里,[left, right)
|
||||
while (left < right) { // 因为left == right的时候,在[left, right)是无效的空间
|
||||
int middle = left + ((right - left) >> 1);
|
||||
if (nums[middle] > target) {
|
||||
right = middle; // target 在左区间,因为是左闭右开的区间,nums[middle]一定不是我们的目标值,所以right = middle,在[left, middle)中继续寻找目标值
|
||||
} else if (nums[middle] < target) {
|
||||
left = middle + 1; // target 在右区间,在 [middle+1, right)中
|
||||
} else { // nums[middle] == target
|
||||
return middle; // 数组中找到目标值的情况,直接返回下标
|
||||
}
|
||||
}
|
||||
return right;
|
||||
}
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## KMP
|
||||
|
||||
```
|
||||
void kmp(int* next, const string& s){
|
||||
next[0] = -1;
|
||||
int j = -1;
|
||||
for(int i = 1; i < s.size(); i++){
|
||||
while (j >= 0 && s[i] != s[j + 1]) {
|
||||
j = next[j];
|
||||
}
|
||||
if (s[i] == s[j + 1]) {
|
||||
j++;
|
||||
}
|
||||
next[i] = j;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 二叉树
|
||||
|
||||
二叉树的定义:
|
||||
|
||||
```
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
```
|
||||
|
||||
### 深度优先遍历(递归)
|
||||
|
||||
前序遍历(中左右)
|
||||
```
|
||||
void traversal(TreeNode* cur, vector<int>& vec) {
|
||||
if (cur == NULL) return;
|
||||
vec.push_back(cur->val); // 中 ,同时也是处理节点逻辑的地方
|
||||
traversal(cur->left, vec); // 左
|
||||
traversal(cur->right, vec); // 右
|
||||
}
|
||||
```
|
||||
中序遍历(左中右)
|
||||
```
|
||||
void traversal(TreeNode* cur, vector<int>& vec) {
|
||||
if (cur == NULL) return;
|
||||
traversal(cur->left, vec); // 左
|
||||
vec.push_back(cur->val); // 中 ,同时也是处理节点逻辑的地方
|
||||
traversal(cur->right, vec); // 右
|
||||
}
|
||||
```
|
||||
中序遍历(中左右)
|
||||
```
|
||||
void traversal(TreeNode* cur, vector<int>& vec) {
|
||||
if (cur == NULL) return;
|
||||
vec.push_back(cur->val); // 中 ,同时也是处理节点逻辑的地方
|
||||
traversal(cur->left, vec); // 左
|
||||
traversal(cur->right, vec); // 右
|
||||
}
|
||||
```
|
||||
|
||||
### 深度优先遍历(迭代法)
|
||||
|
||||
相关题解:[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md)
|
||||
|
||||
前序遍历(中左右)
|
||||
```
|
||||
vector<int> preorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
stack<TreeNode*> st;
|
||||
if (root != NULL) st.push(root);
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop();
|
||||
if (node->right) st.push(node->right); // 右
|
||||
if (node->left) st.push(node->left); // 左
|
||||
st.push(node); // 中
|
||||
st.push(NULL);
|
||||
} else {
|
||||
st.pop();
|
||||
node = st.top();
|
||||
st.pop();
|
||||
result.push_back(node->val); // 节点处理逻辑
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
中序遍历(左中右)
|
||||
```
|
||||
vector<int> inorderTraversal(TreeNode* root) {
|
||||
vector<int> result; // 存放中序遍历的元素
|
||||
stack<TreeNode*> st;
|
||||
if (root != NULL) st.push(root);
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop();
|
||||
if (node->right) st.push(node->right); // 右
|
||||
st.push(node); // 中
|
||||
st.push(NULL);
|
||||
if (node->left) st.push(node->left); // 左
|
||||
} else {
|
||||
st.pop();
|
||||
node = st.top();
|
||||
st.pop();
|
||||
result.push_back(node->val); // 节点处理逻辑
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
后序遍历(左右中)
|
||||
```
|
||||
vector<int> postorderTraversal(TreeNode* root) {
|
||||
vector<int> result;
|
||||
stack<TreeNode*> st;
|
||||
if (root != NULL) st.push(root);
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop();
|
||||
st.push(node); // 中
|
||||
st.push(NULL);
|
||||
if (node->right) st.push(node->right); // 右
|
||||
if (node->left) st.push(node->left); // 左
|
||||
|
||||
} else {
|
||||
st.pop();
|
||||
node = st.top();
|
||||
st.pop();
|
||||
result.push_back(node->val); // 节点处理逻辑
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
### 广度优先遍历(队列)
|
||||
|
||||
相关题解:[0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md)
|
||||
|
||||
```
|
||||
vector<vector<int>> levelOrder(TreeNode* root) {
|
||||
queue<TreeNode*> que;
|
||||
if (root != NULL) que.push(root);
|
||||
vector<vector<int>> result;
|
||||
while (!que.empty()) {
|
||||
int size = que.size();
|
||||
vector<int> vec;
|
||||
for (int i = 0; i < size; i++) {// 这里一定要使用固定大小size,不要使用que.size()
|
||||
TreeNode* node = que.front();
|
||||
que.pop();
|
||||
vec.push_back(node->val); // 节点处理的逻辑
|
||||
if (node->left) que.push(node->left);
|
||||
if (node->right) que.push(node->right);
|
||||
}
|
||||
result.push_back(vec);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
可以直接解决如下题目:
|
||||
|
||||
* [0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md)
|
||||
* [0199.二叉树的右视图](https://github.com/youngyangyang04/leetcode/blob/master/problems/0199.二叉树的右视图.md)
|
||||
* [0637.二叉树的层平均值](https://github.com/youngyangyang04/leetcode/blob/master/problems/0637.二叉树的层平均值.md)
|
||||
* [0104.二叉树的最大深度 (迭代法)](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md)
|
||||
|
||||
* [0111.二叉树的最小深度(迭代法)]((https://github.com/youngyangyang04/leetcode/blob/master/problems/0111.二叉树的最小深度.md))
|
||||
* [0222.完全二叉树的节点个数(迭代法)](https://github.com/youngyangyang04/leetcode/blob/master/problems/0222.完全二叉树的节点个数.md)
|
||||
|
||||
### 二叉树深度
|
||||
|
||||
```
|
||||
int getDepth(TreeNode* node) {
|
||||
if (node == NULL) return 0;
|
||||
return 1 + max(getDepth(node->left), getDepth(node->right));
|
||||
}
|
||||
```
|
||||
|
||||
### 二叉树节点数量
|
||||
|
||||
```
|
||||
int countNodes(TreeNode* root) {
|
||||
if (root == NULL) return 0;
|
||||
return 1 + countNodes(root->left) + countNodes(root->right);
|
||||
}
|
||||
```
|
||||
|
||||
## 回溯算法
|
||||
|
||||
```
|
||||
backtracking() {
|
||||
if (终止条件) {
|
||||
存放结果;
|
||||
}
|
||||
|
||||
for (选择:选择列表(可以想成树中节点孩子的数量)) {
|
||||
递归,处理节点;
|
||||
backtracking();
|
||||
回溯,撤销处理结果
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
(持续补充ing)
|
||||
[各类基础算法模板](https://github.com/youngyangyang04/leetcode/blob/master/problems/算法模板.md)
|
||||
|
||||
# LeetCode 最强题解:
|
||||
|
||||
@@ -469,6 +230,7 @@ backtracking() {
|
||||
|[0617.合并二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0617.合并二叉树.md) |树 |简单|**递归** **迭代**|
|
||||
|[0637.二叉树的层平均值](https://github.com/youngyangyang04/leetcode/blob/master/problems/0637.二叉树的层平均值.md) |树 |简单|**广度优先搜索/队列**|
|
||||
|[0654.最大二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0654.最大二叉树.md) |树 |中等|**递归**|
|
||||
|[0685.冗余连接II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0685.冗余连接II.md) | 并查集/树/图 |困难|**并查集**|
|
||||
|[0700.二叉搜索树中的搜索](https://github.com/youngyangyang04/leetcode/blob/master/problems/0700.二叉搜索树中的搜索.md) |树 |简单|**递归** **迭代**|
|
||||
|[0701.二叉搜索树中的插入操作](https://github.com/youngyangyang04/leetcode/blob/master/problems/0701.二叉搜索树中的插入操作.md) |树 |简单|**递归** **迭代**|
|
||||
|[0705.设计哈希集合](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|
|
||||
|
||||
Reference in New Issue
Block a user