Skip to content

Commit 90a48a7

Browse files
committed
一刷1382
1 parent 23cee80 commit 90a48a7

File tree

7 files changed

+94
-32
lines changed

7 files changed

+94
-32
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9700,12 +9700,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
97009700
//|Medium
97019701
//|
97029702

9703-
//|{counter:codes}
9704-
//|{leetcode_base_url}/balance-a-binary-search-tree/[1382. Balance a Binary Search Tree^]
9705-
//|{source_base_url}/_1382_BalanceABinarySearchTree.java[Java]
9706-
//|{doc_base_url}/1382-balance-a-binary-search-tree.adoc[题解]
9707-
//|Medium
9708-
//|
9703+
|{counter:codes}
9704+
|{leetcode_base_url}/balance-a-binary-search-tree/[1382. Balance a Binary Search Tree^]
9705+
|{source_base_url}/_1382_BalanceABinarySearchTree.java[Java]
9706+
|{doc_base_url}/1382-balance-a-binary-search-tree.adoc[题解]
9707+
|Medium
9708+
|
97099709

97109710
//|{counter:codes}
97119711
//|{leetcode_base_url}/maximum-performance-of-a-team/[1383. Maximum Performance of a Team^]
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
[#1382-balance-a-binary-search-tree]
2-
= 1382. Balance a Binary Search Tree
2+
= 1382. 将二叉搜索树变平衡
33

4-
{leetcode}/problems/balance-a-binary-search-tree/[LeetCode - 1382. Balance a Binary Search Tree ^]
4+
https://leetcode.cn/problems/balance-a-binary-search-tree/[LeetCode - 1382. 将二叉搜索树变平衡 ^]
55

6-
Given the `root` of a binary search tree, return _a *balanced* binary search tree with the same node values_. If there is more than one answer, return *any of them*.
6+
给你一棵二叉搜索树,请你返回一棵 *平衡后* 的二叉搜索树,新生成的树应该与原来的树有着相同的节点值。如果有多种构造方法,请你返回任意一种。
77

8-
A binary search tree is *balanced* if the depth of the two subtrees of every node never differs by more than `1`.
8+
如果一棵二叉搜索树中,每个节点的两棵子树高度差不超过 `1`,我们就称这棵二叉搜索树是 *平衡的*
99

10-
11-
*Example 1:*
12-
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg" style="width: 500px; height: 319px;" />
13-
[subs="verbatim,quotes"]
14-
----
15-
*Input:* root = [1,null,2,null,3,null,4,null,null]
16-
*Output:* [2,1,3,null,null,null,4]
17-
*Explanation:* This is not the only correct answer, [3,1,4,null,2] is also correct.
18-
----
10+
*示例 1:*
1911

20-
*Example 2:*
21-
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg" style="width: 224px; height: 145px;" />
22-
[subs="verbatim,quotes"]
23-
----
24-
*Input:* root = [2,1,3]
25-
*Output:* [2,1,3]
26-
----
12+
image::images/1382-01.jpg[{image_attr}]
2713

28-
29-
*Constraints:*
14+
....
15+
输入:root = [1,null,2,null,3,null,4,null,null]
16+
输出:[2,1,3,null,null,null,4]
17+
解释:这不是唯一的正确答案,[3,1,4,null,2,null,null] 也是一个可行的构造方案。
18+
....
3019

20+
*示例 2:*
3121

32-
* The number of nodes in the tree is in the range `[1, 10^4^]`.
33-
* `1 <= Node.val <= 10^5^`
22+
image::images/1382-02.jpg[{image_attr}]
3423

24+
....
25+
输入: root = [2,1,3]
26+
输出: [2,1,3]
27+
....
28+
29+
30+
*提示:*
31+
32+
* 树节点的数目在 `[1, 10^4^]` 范围内。
33+
* `1 \<= Node.val \<= 10^5^`
3534
3635
3736
3837
== 思路分析
3938

39+
二叉搜索树,先中序遍历拿到所有节点,然后递归构造平衡二叉搜索树。
4040

4141
[[src-1382]]
4242
[tabs]
@@ -63,4 +63,6 @@ include::{sourcedir}/_1382_BalanceABinarySearchTree.java[tag=answer]
6363

6464
== 参考资料
6565

66-
66+
. https://leetcode.cn/problems/balance-a-binary-search-tree/solutions/150820/shou-si-avlshu-wo-bu-guan-wo-jiu-shi-yao-xuan-zhua/[1382. 将二叉搜索树变平衡 - 手撕AVL树,我不管,我就是要旋转^]
67+
. https://leetcode.cn/problems/balance-a-binary-search-tree/solutions/457827/1382-jiang-er-cha-sou-suo-shu-bian-ping-heng-gou-z/[1382. 将二叉搜索树变平衡 - 「代码随想录」1382. 将二叉搜索树变平衡:【构造平衡二叉搜索树】详解^]
68+
. https://leetcode.cn/problems/balance-a-binary-search-tree/solutions/241897/jiang-er-cha-sou-suo-shu-bian-ping-heng-by-leetcod/[1382. 将二叉搜索树变平衡 - 官方题解^]

docs/images/1382-01.jpg

26.5 KB
Loading

docs/images/1382-02.jpg

6.29 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,7 @@ include::1362-closest-divisors.adoc[leveloffset=+1]
28472847

28482848
// include::1381-design-a-stack-with-increment-operation.adoc[leveloffset=+1]
28492849

2850-
// include::1382-balance-a-binary-search-tree.adoc[leveloffset=+1]
2850+
include::1382-balance-a-binary-search-tree.adoc[leveloffset=+1]
28512851

28522852
// include::1383-maximum-performance-of-a-team.adoc[leveloffset=+1]
28532853

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,11 @@ endif::[]
883883
|{doc_base_url}/1023-camelcase-matching.adoc[题解]
884884
|✅ 双指针
885885

886+
|{counter:codes2503}
887+
|{leetcode_base_url}/balance-a-binary-search-tree/[1382. 将二叉搜索树变平衡^]
888+
|{doc_base_url}/1382-balance-a-binary-search-tree.adoc[题解]
889+
|✅ 二叉搜索树,先中序遍历拿到所有节点,然后递归构造平衡二叉搜索树。
890+
886891
|===
887892

888893
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
import com.diguage.util.TreeNodes;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class _1382_BalanceABinarySearchTree {
11+
// tag::answer[]
12+
/**
13+
* @author D瓜哥 · https://www.diguage.com
14+
* @since 2025-05-31 22:12:20
15+
*/
16+
public TreeNode balanceBST(TreeNode root) {
17+
List<TreeNode> data = new ArrayList<>();
18+
TreeNode cur = root;
19+
while (cur != null) {
20+
TreeNode mostRight = cur.left;
21+
if (mostRight != null) {
22+
while (mostRight.right != null && mostRight.right != cur) {
23+
mostRight = mostRight.right;
24+
}
25+
if (mostRight.right == null) {
26+
mostRight.right = cur;
27+
cur = cur.left;
28+
continue;
29+
} else {
30+
mostRight.right = null;
31+
}
32+
}
33+
data.add(cur);
34+
cur = cur.right;
35+
}
36+
TreeNode result = dfs(data, 0, data.size() - 1);
37+
return result;
38+
}
39+
40+
private TreeNode dfs(List<TreeNode> data, int start, int end) {
41+
if (start > end) {
42+
return null;
43+
}
44+
int mid = start + (end - start) / 2;
45+
TreeNode node = data.get(mid);
46+
node.left = dfs(data, start, mid - 1);
47+
node.right = dfs(data, mid + 1, end);
48+
return node;
49+
}
50+
// end::answer[]
51+
public static void main(String[] args) {
52+
new _1382_BalanceABinarySearchTree()
53+
.balanceBST(TreeNodes.buildTree(Arrays.asList(4, 2, 6, 1, 3, 5, 7)));
54+
}
55+
}

0 commit comments

Comments
 (0)