先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Golang全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip1024b (备注go)
正文
下面用图解来说明一下前序遍历算法的递归过程。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t649bQ1H-1631410485856)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906222707569.png#pic_center)]
)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WJP8QBir-1631410165737)(
104. 二叉树的最大深度
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hokROzkn-1631410165737)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906225439631.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c6vkvJPE-1631410165738)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906230105322.png)]
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode *root) {
if (root == NULL) return 0;
return fmax(maxDepth(root->left), maxDepth(root->right)) + 1;
}
/**
* 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;
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K6grKsYv-1631410165738)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906230159673.png)]
struct QueNode {
struct TreeNode *p;
struct QueNode *next;
};
void init(struct QueNode **p, struct TreeNode *t) {
(*p) = (struct QueNode *)malloc(sizeof(struct QueNode));
(*p)->p = t;
(*p)->next = NULL;
}
int maxDepth(struct TreeNode *root) {
if (root == NULL) return 0;
struct QueNode *left, *right;
init(&left, root);
right = left;
int ans = 0, sz = 1, tmp = 0;
while (left != NULL) {
tmp = 0;
while (sz > 0) {
if (left->p->left != NULL) {
init(&right->next, left->p->left);
right = right->next;
tmp++;
}
if (left->p->right != NULL) {
init(&right->next, left->p->right);
right = right->next;
tmp++;
}
left = left->next;
sz–;
}
sz += tmp;
ans++;
}
return ans;
}
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
queue<TreeNode*> Q;
Q.push(root);
int ans = 0;
while (!Q.empty()) {
int sz = Q.size();
while (sz > 0) {
TreeNode* node = Q.front();Q.pop();
if (node->left) Q.push(node->left);
if (node->right) Q.push(node->right);
sz -= 1;
}
ans += 1;
}
return ans;
}
};
102. 二叉树的层序遍历
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FSQWBJz6-1631410165739)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906230630256.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hVrPvVav-1631410165739)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906230809581.png)]
/**
* 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<vector> levelOrder(TreeNode* root) {
vector <vector > ret;
if (!root) {
return ret;
}
queue <TreeNode*> q;
q.push(root);
while (!q.empty()) {
int currentLevelSize = q.size();
ret.push_back(vector ());
for (int i = 1; i <= currentLevelSize; ++i) {
auto node = q.front(); q.pop();
ret.back().push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return ret;
}
};
94. 二叉树的中序遍历
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-haY1LbiS-1631410165739)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906231012868.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eKamUhz6-1631410165740)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906231108467.png)]
void inorder(struct TreeNode* root, int* res, int* resSize) {
if (!root) {
return;
}
inorder(root->left, res, resSize);
res[(*resSize)++] = root->val;
inorder(root->right, res, resSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 501);
*returnSize = 0;
inorder(root, res, returnSize);
return res;
}
class Solution {
public:
void inorder(TreeNode* root, vector& res) {
if (!root) {
return;
}
inorder(root->left, res);
res.push_back(root->val);
inorder(root->right, res);
}
vector inorderTraversal(TreeNode* root) {
vector res;
inorder(root, res);
return res;
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1ykLuVUp-1631410165741)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906231203588.png)]
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
*returnSize = 0;
int* res = malloc(sizeof(int) * 501);
struct TreeNode** stk = malloc(sizeof(struct TreeNode*) * 501);
int top = 0;
while (root != NULL || top > 0) {
while (root != NULL) {
stk[top++] = root;
root = root->left;
}
root = stk[–top];
res[(*returnSize)++] = root->val;
root = root->right;
}
return res;
}
Solution {
public:
vector inorderTraversal(TreeNode* root) {
vector res;
stack<TreeNode*> stk;
while (root != nullptr || !stk.empty()) {
while (root != nullptr) {
stk.push(root);
root = root->left;
}
root = stk.top();
stk.pop();
res.push_back(root->val);
root = root->right;
}
return res;
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dOsyM3uB-1631410165741)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906231316031.png)]
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 501);
*returnSize = 0;
struct TreeNode* predecessor = NULL;
while (root != NULL) {
if (root->left != NULL) {
// predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
predecessor = root->left;
while (predecessor->right != NULL && predecessor->right != root) {
predecessor = predecessor->right;
}
// 让 predecessor 的右指针指向 root,继续遍历左子树
if (predecessor->right == NULL) {
predecessor->right = root;
root = root->left;
}
// 说明左子树已经访问完了,我们需要断开链接
else {
res[(*returnSize)++] = root->val;
predecessor->right = NULL;
root = root->right;
}
}
// 如果没有左孩子,则直接访问右孩子
else {
res[(*returnSize)++] = root->val;
root = root->right;
}
}
return res;
}
class Solution {
public:
vector inorderTraversal(TreeNode* root) {
vector res;
TreeNode *predecessor = nullptr;
while (root != nullptr) {
if (root->left != nullptr) {
// predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
predecessor = root->left;
while (predecessor->right != nullptr && predecessor->right != root) {
predecessor = predecessor->right;
}
// 让 predecessor 的右指针指向 root,继续遍历左子树
if (predecessor->right == nullptr) {
predecessor->right = root;
root = root->left;
}
// 说明左子树已经访问完了,我们需要断开链接
else {
res.push_back(root->val);
predecessor->right = nullptr;
root = root->right;
}
}
// 如果没有左孩子,则直接访问右孩子
else {
res.push_back(root->val);
root = root->right;
}
}
return res;
}
};
101. 对称二叉树
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DDaijCXA-1631410165741)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906231723650.png)]
class Solution {
public:
bool check(TreeNode *p, TreeNode *q) {
if (!p && !q) return true;
if (!p || !q) return false;
return p->val == q->val && check(p->left, q->right) && check(p->right, q->left);
}
bool isSymmetric(TreeNode* root) {
return check(root, root);
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TJOn4n2k-1631410165742)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906231838027.png)]
class Solution {
public:
bool check(TreeNode *u, TreeNode *v) {
queue <TreeNode*> q;
q.push(u); q.push(v);
while (!q.empty()) {
u = q.front(); q.pop();
v = q.front(); q.pop();
if (!u && !v) continue;
if ((!u || !v) || (u->val != v->val)) return false;
q.push(u->left);
q.push(v->right);
q.push(u->right);
q.push(v->left);
}
return true;
}
bool isSymmetric(TreeNode* root) {
return check(root, root);
}
};
98. 验证二叉搜索树
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XEZkgoUx-1631410165742)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906232046142.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VP8vQGV3-1631410165743)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906232733702.png)]
class Solution {
public:
bool helper(TreeNode* root, long long lower, long long upper) {
if (root == nullptr) {
return true;
}
if (root -> val <= lower || root -> val >= upper) {
return false;
}
return helper(root -> left, lower, root -> val) && helper(root -> right, root -> val, upper);
}
bool isValidBST(TreeNode* root) {
return helper(root, LONG_MIN, LONG_MAX);
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YXztDcbC-1631410165743)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906232758657.png)]
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> stack;
long long inorder = (long long)INT_MIN - 1;
while (!stack.empty() || root != nullptr) {
while (root != nullptr) {
stack.push(root);
root = root -> left;
}
root = stack.top();
stack.pop();
// 如果中序遍历得到的节点的值小于等于前一个 inorder,说明不是二叉搜索树
if (root -> val <= inorder) {
return false;
}
inorder = root -> val;
root = root -> right;
}
return true;
}
};
100. 相同的树
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9x97yxnb-1631410165743)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906232905376.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tpraHkvi-1631410165744)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906232942970.png)]
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if (p == NULL && q == NULL) {
return true;
} else if (p == NULL || q == NULL) {
return false;
} else if (p->val != q->val) {
return false;
} else {
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
}
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (p == nullptr && q == nullptr) {
return true;
} else if (p == nullptr || q == nullptr) {
return false;
} else if (p->val != q->val) {
return false;
} else {
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7oaAoo5M-1631410165744)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233029546.png)]
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if (p == NULL && q == NULL) {
return true;
} else if (p == NULL || q == NULL) {
return false;
}
struct TreeNode** que1 = (struct TreeNode**)malloc(sizeof(struct TreeNode*));
struct TreeNode** que2 = (struct TreeNode**)malloc(sizeof(struct TreeNode*));
int queleft1 = 0, queright1 = 0;
int queleft2 = 0, queright2 = 0;
que1[queright1++] = p;
que2[queright2++] = q;
while (queleft1 < queright1 && queleft2 < queright2) {
struct TreeNode* node1 = que1[queleft1++];
struct TreeNode* node2 = que2[queleft2++];
if (node1->val != node2->val) {
return false;
}
struct TreeNode* left1 = node1->left;
struct TreeNode* right1 = node1->right;
struct TreeNode* left2 = node2->left;
struct TreeNode* right2 = node2->right;
if ((left1 == NULL) ^ (left2 == NULL)) {
return false;
}
if ((right1 == NULL) ^ (right2 == NULL)) {
return false;
}
if (left1 != NULL) {
queright1++;
que1 = realloc(que1, sizeof(struct TreeNode*) * queright1);
que1[queright1 - 1] = left1;
}
if (right1 != NULL) {
queright1++;
que1 = realloc(que1, sizeof(struct TreeNode*) * queright1);
que1[queright1 - 1] = right1;
}
if (left2 != NULL) {
queright2++;
que2 = realloc(que2, sizeof(struct TreeNode*) * queright2);
que2[queright2 - 1] = left2;
}
if (right2 != NULL) {
queright2++;
que2 = realloc(que2, sizeof(struct TreeNode*) * queright2);
que2[queright2 - 1] = right2;
}
}
return queleft1 == queright1 && queleft2 == queright2;
}
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (p == nullptr && q == nullptr) {
return true;
} else if (p == nullptr || q == nullptr) {
return false;
}
queue <TreeNode*> queue1, queue2;
queue1.push§;
queue2.push(q);
while (!queue1.empty() && !queue2.empty()) {
auto node1 = queue1.front();
queue1.pop();
auto node2 = queue2.front();
queue2.pop();
if (node1->val != node2->val) {
return false;
}
auto left1 = node1->left, right1 = node1->right, left2 = node2->left, right2 = node2->right;
if ((left1 == nullptr) ^ (left2 == nullptr)) {
return false;
}
if ((right1 == nullptr) ^ (right2 == nullptr)) {
return false;
}
if (left1 != nullptr) {
queue1.push(left1);
}
if (right1 != nullptr) {
queue1.push(right1);
}
if (left2 != nullptr) {
queue2.push(left2);
}
if (right2 != nullptr) {
queue2.push(right2);
}
}
return queue1.empty() && queue2.empty();
}
};
226. 翻转二叉树
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eHkkuHEY-1631410165745)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233211449.png)]
struct TreeNode* invertTree(struct TreeNode* root) {
if (root == NULL) {
return NULL;
}
struct TreeNode* left = invertTree(root->left);
struct TreeNode* right = invertTree(root->right);
root->left = right;
root->right = left;
return root;
}
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) {
return nullptr;
}
TreeNode* left = invertTree(root->left);
TreeNode* right = invertTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
144. 二叉树的前序遍历
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IVY2Xsww-1631410165745)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233405175.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VqSyQqMO-1631410165746)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233442557.png)]
void preorder(struct TreeNode* root, int* res, int* resSize) {
if (root == NULL) {
return;
}
res[(*resSize)++] = root->val;
preorder(root->left, res, resSize);
preorder(root->right, res, resSize);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 2000);
*returnSize = 0;
preorder(root, res, returnSize);
return res;
}
class Solution {
public:
void preorder(TreeNode *root, vector &res) {
if (root == nullptr) {
return;
}
res.push_back(root->val);
preorder(root->left, res);
preorder(root->right, res);
}
vector preorderTraversal(TreeNode *root) {
vector res;
preorder(root, res);
return res;
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Ui2W7OE-1631410165746)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233539153.png)]
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 2000);
*returnSize = 0;
if (root == NULL) {
return res;
}
struct TreeNode* stk[2000];
struct TreeNode* node = root;
int stk_top = 0;
while (stk_top > 0 || node != NULL) {
while (node != NULL) {
res[(*returnSize)++] = node->val;
stk[stk_top++] = node;
node = node->left;
}
node = stk[–stk_top];
node = node->right;
}
return res;
}
class Solution {
public:
vector preorderTraversal(TreeNode* root) {
vector res;
if (root == nullptr) {
return res;
}
stack<TreeNode*> stk;
TreeNode* node = root;
while (!stk.empty() || node != nullptr) {
while (node != nullptr) {
res.emplace_back(node->val);
stk.emplace(node);
node = node->left;
}
node = stk.top();
stk.pop();
node = node->right;
}
return res;
}
};
111. 二叉树的最小深度
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ccT0q5XO-1631410165747)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233645598.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x0EskIlp-1631410165747)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233711706.png)]
int minDepth(struct TreeNode *root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
int min_depth = INT_MAX;
if (root->left != NULL) {
min_depth = fmin(minDepth(root->left), min_depth);
}
if (root->right != NULL) {
min_depth = fmin(minDepth(root->right), min_depth);
}
return min_depth + 1;
}
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;
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ydP328lE-1631410165748)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906233802403.png)]
typedef struct {
int val;
struct TreeNode *node;
struct queNode *next;
} queNode;
void init(queNode **p, int val, struct TreeNode *node) {
(*p) = (queNode *)malloc(sizeof(queNode));
(*p)->val = val;
(*p)->node = node;
(*p)->next = NULL;
}
int minDepth(struct TreeNode *root) {
if (root == NULL) {
return 0;
}
queNode *queLeft, *queRight;
init(&queLeft, 1, root);
queRight = queLeft;
while (queLeft != NULL) {
struct TreeNode *node = queLeft->node;
int depth = queLeft->val;
if (node->left == NULL && node->right == NULL) {
return depth;
}
if (node->left != NULL) {
init(&queRight->next, depth + 1, node->left);
queRight = queRight->next;
}
if (node->right != NULL) {
init(&queRight->next, depth + 1, node->right);
queRight = queRight->next;
}
queLeft = queLeft->next;
}
return false;
}
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == nullptr) {
return 0;
}
queue<pair<TreeNode *, int> > que;
que.emplace(root, 1);
while (!que.empty()) {
TreeNode *node = que.front().first;
int depth = que.front().second;
que.pop();
if (node->left == nullptr && node->right == nullptr) {
return depth;
}
if (node->left != nullptr) {
que.emplace(node->left, depth + 1);
}
if (node->right != nullptr) {
que.emplace(node->right, depth + 1);
}
}
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
it(&queRight->next, depth + 1, node->right);
queRight = queRight->next;
}
queLeft = queLeft->next;
}
return false;
}
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == nullptr) {
return 0;
}
queue<pair<TreeNode *, int> > que;
que.emplace(root, 1);
while (!que.empty()) {
TreeNode *node = que.front().first;
int depth = que.front().second;
que.pop();
if (node->left == nullptr && node->right == nullptr) {
return depth;
}
if (node->left != nullptr) {
que.emplace(node->left, depth + 1);
}
if (node->right != nullptr) {
que.emplace(node->right, depth + 1);
}
}
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-iZwmgyZH-1713176327910)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!