Skip to content

Commit 39b4bff

Browse files
committed
new Blog
1 parent c47e9c2 commit 39b4bff

13 files changed

Lines changed: 2226 additions & 89 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Kafka消息队列笔记()
3+
published: 2025-11-01
4+
updated: 2025-11-01
5+
description: ''
6+
image: ''
7+
tags: [MQ]
8+
category: ''
9+
draft: false
10+
---
11+
12+
# Kafka笔记
13+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Kafka消息队列笔记()
3+
published: 2025-11-13
4+
updated: 2025-11-13
5+
description: ''
6+
image: ''
7+
tags: [MQ]
8+
category: ''
9+
draft: false
10+
---
11+
12+
# Kafka笔记
13+
14+
Kafka消息队列应用场景:异步处理、系统解耦、流量削峰、日志处理
15+
16+
交互模型:请求响应模型、生产者消费者模型
17+
18+
消息队列两种模式:点对点模式、发布订阅模型
19+
20+
21+
22+
Producers:将消息放入集群中
23+
24+
Consumers:将消息数据从集群中拉出来
25+
26+
Connectors:连接器可以将数据库中的数据导入Kafka,也可以将数据导出到数据库中
27+
28+
Stream Processors:流处理器可以从Kafka中拉取数据,也可以将数据写入Kafka
29+
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
title: LeetCode刷题(二叉搜索树最小绝对差、搜索树第K小元素)
3+
published: 2025-11-13
4+
updated: 2025-11-13
5+
description: '二叉搜索树最小绝对差、搜索树第K小元素'
6+
image: ''
7+
tags: [LeetCode]
8+
category: 'LeetCode'
9+
draft: false
10+
---
11+
12+
# 二叉搜索树最小绝对差
13+
14+
## 题目描述
15+
16+
给你一个二叉搜索树的根节点 `root` ,返回 **树中任意两不同节点值之间的最小差值**
17+
18+
差值是一个正数,其数值等于两值之差的绝对值。
19+
20+
**示例 1:**
21+
22+
![361](../images/361.jpg)
23+
24+
```
25+
输入:root = [4,2,6,1,3]
26+
输出:1
27+
```
28+
29+
**示例 2:**
30+
31+
![362](../images/362.jpg)
32+
33+
```
34+
输入:root = [1,0,48,null,null,12,49]
35+
输出:1
36+
```
37+
38+
39+
40+
## 题解
41+
42+
用pre记录前一个值,在中序遍历中进行对于result迭代
43+
44+
```java
45+
class Solution {
46+
int result = Integer.MAX_VALUE;
47+
int pre = -1;
48+
public int getMinimumDifference(TreeNode root) {
49+
dfs(root);
50+
return result;
51+
}
52+
public void dfs(TreeNode root) {
53+
if(root == null) return;
54+
dfs(root.left);
55+
if(pre != -1) {
56+
result = Math.min(result, Math.abs(pre - root.val));
57+
}
58+
pre = root.val;
59+
dfs(root.right);
60+
}
61+
}
62+
```
63+
64+
补一个非递归的方式
65+
66+
```java
67+
class Solution {
68+
int result = Integer.MAX_VALUE;
69+
int pre = -1;
70+
public int getMinimumDifference(TreeNode root) {
71+
Stack<TreeNode> stack = new Stack();
72+
TreeNode p = root;
73+
while(!stack.isEmpty() || p != null) {
74+
while(p != null) {
75+
stack.push(p);
76+
p = p.left;
77+
}
78+
TreeNode node = stack.pop();
79+
if(pre != -1) {
80+
result = Math.min(result, Math.abs(node.val - pre));
81+
}
82+
pre = node.val;
83+
p = node.right;
84+
}
85+
return result;
86+
}
87+
}
88+
```
89+
90+
91+
92+
# 搜索树第K小元素
93+
94+
## 题目描述
95+
96+
给定一个二叉搜索树的根节点 `root` ,和一个整数 `k` ,请你设计一个算法查找其中第 `k` 小的元素(从 1 开始计数)。
97+
98+
**示例 1:**
99+
100+
![363](../images/363.jpg)
101+
102+
```
103+
输入:root = [3,1,4,null,2], k = 1
104+
输出:1
105+
```
106+
107+
**示例 2:**
108+
109+
![364](../images/364.jpg)
110+
111+
```
112+
输入:root = [5,3,6,2,4,null,null,1], k = 3
113+
输出:3
114+
```
115+
116+
117+
118+
## 题解
119+
120+
用中序遍历,遍历到数字就加一,如果当前数字在第K为就设置当前数字为result
121+
122+
```java
123+
class Solution {
124+
int num = 1;
125+
int result = -1;
126+
int target;
127+
public int kthSmallest(TreeNode root, int k) {
128+
target = k;
129+
dfs(root);
130+
return result;
131+
}
132+
133+
public void dfs(TreeNode root) {
134+
if(root == null) return;
135+
dfs(root.left);
136+
if(num == target) {
137+
result = root.val;
138+
}
139+
num++;
140+
dfs(root.right);
141+
}
142+
}
143+
```
144+
145+
非递归
146+
147+
```java
148+
class Solution {
149+
public int kthSmallest(TreeNode root, int k) {
150+
Stack<TreeNode> stack = new Stack();
151+
TreeNode p = root;
152+
while(!stack.isEmpty() || p != null) {
153+
while(p != null) {
154+
stack.push(p);
155+
p = p.left;
156+
}
157+
TreeNode node = stack.pop();
158+
k--;
159+
if(k == 0) {
160+
return node.val;
161+
}
162+
p = node.right;
163+
}
164+
return p.val;
165+
}
166+
}
167+
```
168+
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: LeetCode刷题(二叉树右视图、二叉树的锯齿层序遍历)
3+
published: 2025-11-12
4+
updated: 2025-11-12
5+
description: '二叉树右视图、二叉树的锯齿层序遍历'
6+
image: ''
7+
tags: [LeetCode]
8+
category: 'LeetCode'
9+
draft: false
10+
---
11+
12+
# 二叉树右视图
13+
14+
## 题目描述
15+
16+
给定一个二叉树的 **根节点** `root`,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
17+
18+
**示例 1:**
19+
20+
**输入:**root = [1,2,3,null,5,null,4]
21+
22+
**输出:**[1,3,4]
23+
24+
**解释:**
25+
26+
![358](../images/358.png)
27+
28+
**示例 2:**
29+
30+
**输入:**root = [1,2,3,4,null,null,null,5]
31+
32+
**输出:**[1,3,4,5]
33+
34+
**解释:**
35+
36+
![359](../images/359.png)
37+
38+
**示例 3:**
39+
40+
**输入:**root = [1,null,3]
41+
42+
**输出:**[1,3]
43+
44+
**示例 4:**
45+
46+
**输入:**root = []
47+
48+
**输出:**[]
49+
50+
51+
52+
## 题解
53+
54+
通过层序遍历取出最后的元素即可
55+
56+
```java
57+
class Solution {
58+
public List<Integer> rightSideView(TreeNode root) {
59+
List<Integer> result = new ArrayList<>();
60+
if(root == null) return result;
61+
Queue<TreeNode> queue = new LinkedList<>();
62+
queue.offer(root);
63+
while(!queue.isEmpty()) {
64+
int n = queue.size();
65+
for(int i = 0; i < n; i++) {
66+
TreeNode node = queue.poll();
67+
if(i == n - 1) result.add(node.val);
68+
if(node.left != null) queue.offer(node.left);
69+
if(node.right != null) queue.offer(node.right);
70+
}
71+
}
72+
return result;
73+
}
74+
}
75+
```
76+
77+
78+
79+
80+
81+
# 二叉树的锯齿层序遍历
82+
83+
## 题目描述
84+
85+
给你二叉树的根节点 `root` ,返回其节点值的 **锯齿形层序遍历** 。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
86+
87+
**示例 1:**
88+
89+
![360](../images/360.jpg)
90+
91+
```
92+
输入:root = [3,9,20,null,null,15,7]
93+
输出:[[3],[20,9],[15,7]]
94+
```
95+
96+
**示例 2:**
97+
98+
```
99+
输入:root = [1]
100+
输出:[[1]]
101+
```
102+
103+
**示例 3:**
104+
105+
```
106+
输入:root = []
107+
输出:[]
108+
```
109+
110+
111+
112+
## 题解
113+
114+
记录层级在偶数层的时候反转链表即可
115+
116+
```java
117+
class Solution {
118+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
119+
List<List<Integer>> result = new ArrayList<>();
120+
if(root == null) return result;
121+
Queue<TreeNode> queue = new LinkedList<>();
122+
queue.offer(root);
123+
int level = 1;
124+
while(!queue.isEmpty()) {
125+
int n = queue.size();
126+
List<Integer> list = new ArrayList<>();
127+
for(int i = 0; i < n; i++) {
128+
TreeNode node = queue.poll();
129+
list.add(node.val);
130+
if(node.left != null) queue.offer(node.left);
131+
if(node.right != null) queue.offer(node.right);
132+
}
133+
if(level % 2 == 0) {
134+
Collections.reverse(list);
135+
}
136+
result.add(list);
137+
level++;
138+
}
139+
return result;
140+
}
141+
}
142+
```
143+

0 commit comments

Comments
 (0)