Skip to content

Commit d47c2ec

Browse files
committed
Add solution 0530、783
1 parent 8ded561 commit d47c2ec

27 files changed

+705
-96
lines changed

README.md

+70-70
Large diffs are not rendered by default.

leetcode/0004.Median-of-Two-Sorted-Arrays/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You may assume **nums1** and **nums2** cannot be both empty.
3939

4040

4141
- 给出两个有序数组,要求找出这两个数组合并以后的有序数组中的中位数。要求时间复杂度为 O(log (m+n))。
42-
- 这一题最容易想到的办法是把两个数组合并,然后取出中位数。但是合并有序数组的操作是 `O(max(n,m))` 的,不符合题意。看到题目给的 `log` 的时间复杂度,很容易联想到二分搜索。
42+
- 这一题最容易想到的办法是把两个数组合并,然后取出中位数。但是合并有序数组的操作是 `O(m+n)` 的,不符合题意。看到题目给的 `log` 的时间复杂度,很容易联想到二分搜索。
4343
- 由于要找到最终合并以后数组的中位数,两个数组的总大小也知道,所以中间这个位置也是知道的。只需要二分搜索一个数组中切分的位置,另一个数组中切分的位置也能得到。为了使得时间复杂度最小,所以二分搜索两个数组中长度较小的那个数组。
4444
- 关键的问题是如何切分数组 1 和数组 2 。其实就是如何切分数组 1 。先随便二分产生一个 `midA`,切分的线何时算满足了中位数的条件呢?即,线左边的数都小于右边的数,即,`nums1[midA-1] ≤ nums2[midB] && nums2[midB-1] ≤ nums1[midA]` 。如果这些条件都不满足,切分线就需要调整。如果 `nums1[midA] < nums2[midB-1]`,说明 `midA` 这条线划分出来左边的数小了,切分线应该右移;如果 `nums1[midA-1] > nums2[midB]`,说明 midA 这条线划分出来左边的数大了,切分线应该左移。经过多次调整以后,切分线总能找到满足条件的解。
4545
- 假设现在找到了切分的两条线了,`数组 1` 在切分线两边的下标分别是 `midA - 1``midA``数组 2` 在切分线两边的下标分别是 `midB - 1``midB`。最终合并成最终数组,如果数组长度是奇数,那么中位数就是 `max(nums1[midA-1], nums2[midB-1])`。如果数组长度是偶数,那么中间位置的两个数依次是:`max(nums1[midA-1], nums2[midB-1])``min(nums1[midA], nums2[midB])`,那么中位数就是 `(max(nums1[midA-1], nums2[midB-1]) + min(nums1[midA], nums2[midB])) / 2`。图示见下图:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"math"
5+
6+
"github.com/halfrost/LeetCode-Go/structures"
7+
)
8+
9+
// TreeNode define
10+
type TreeNode = structures.TreeNode
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* type TreeNode struct {
15+
* Val int
16+
* Left *TreeNode
17+
* Right *TreeNode
18+
* }
19+
*/
20+
21+
func getMinimumDifference(root *TreeNode) int {
22+
res, nodes := math.MaxInt16, -1
23+
dfsBST(root, &res, &nodes)
24+
return res
25+
}
26+
27+
func dfsBST(root *TreeNode, res, pre *int) {
28+
if root == nil {
29+
return
30+
}
31+
dfsBST(root.Left, res, pre)
32+
if *pre != -1 {
33+
*res = min(*res, abs(root.Val-*pre))
34+
}
35+
*pre = root.Val
36+
dfsBST(root.Right, res, pre)
37+
}
38+
39+
func min(a, b int) int {
40+
if a > b {
41+
return b
42+
}
43+
return a
44+
}
45+
46+
func abs(a int) int {
47+
if a > 0 {
48+
return a
49+
}
50+
return -a
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question530 struct {
11+
para530
12+
ans530
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para530 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans530 struct {
24+
one int
25+
}
26+
27+
func Test_Problem530(t *testing.T) {
28+
29+
qs := []question530{
30+
31+
{
32+
para530{[]int{4, 2, 6, 1, 3}},
33+
ans530{1},
34+
},
35+
36+
{
37+
para530{[]int{1, 0, 48, structures.NULL, structures.NULL, 12, 49}},
38+
ans530{1},
39+
},
40+
41+
{
42+
para530{[]int{90, 69, structures.NULL, 49, 89, structures.NULL, 52}},
43+
ans530{1},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 530------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans530, q.para530
51+
fmt.Printf("【input】:%v ", p)
52+
rootOne := structures.Ints2TreeNode(p.one)
53+
fmt.Printf("【output】:%v \n", getMinimumDifference(rootOne))
54+
}
55+
fmt.Printf("\n\n\n")
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [530. Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)
2+
3+
4+
## 题目
5+
6+
Given a binary search tree with non-negative values, find the minimum [absolute difference](https://en.wikipedia.org/wiki/Absolute_difference) between values of any two nodes.
7+
8+
**Example:**
9+
10+
```
11+
Input:
12+
13+
1
14+
\
15+
3
16+
/
17+
2
18+
19+
Output:
20+
1
21+
22+
Explanation:
23+
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
24+
```
25+
26+
**Note:**
27+
28+
- There are at least two nodes in this BST.
29+
- This question is the same as 783: [https://leetcode.com/problems/minimum-distance-between-bst-nodes/](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)
30+
31+
## 题目大意
32+
33+
给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。
34+
35+
## 解题思路
36+
37+
- 由于是 BST 树,利用它有序的性质,中根遍历的结果是有序的。中根遍历过程中动态维护前后两个节点的差值,即可找到最小差值。
38+
- 此题与第 783 题完全相同。
39+
40+
## 代码
41+
42+
```go
43+
package leetcode
44+
45+
import (
46+
"math"
47+
48+
"github.com/halfrost/LeetCode-Go/structures"
49+
)
50+
51+
// TreeNode define
52+
type TreeNode = structures.TreeNode
53+
54+
/**
55+
* Definition for a binary tree node.
56+
* type TreeNode struct {
57+
* Val int
58+
* Left *TreeNode
59+
* Right *TreeNode
60+
* }
61+
*/
62+
63+
func getMinimumDifference(root *TreeNode) int {
64+
res, nodes := math.MaxInt16, -1
65+
dfsBST(root, &res, &nodes)
66+
return res
67+
}
68+
69+
func dfsBST(root *TreeNode, res, pre *int) {
70+
if root == nil {
71+
return
72+
}
73+
dfsBST(root.Left, res, pre)
74+
if *pre != -1 {
75+
*res = min(*res, abs(root.Val-*pre))
76+
}
77+
*pre = root.Val
78+
dfsBST(root.Right, res, pre)
79+
}
80+
81+
func min(a, b int) int {
82+
if a > b {
83+
return b
84+
}
85+
return a
86+
}
87+
88+
func abs(a int) int {
89+
if a > 0 {
90+
return a
91+
}
92+
return -a
93+
}
94+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"math"
5+
6+
"github.com/halfrost/LeetCode-Go/structures"
7+
)
8+
9+
// TreeNode define
10+
type TreeNode = structures.TreeNode
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* type TreeNode struct {
15+
* Val int
16+
* Left *TreeNode
17+
* Right *TreeNode
18+
* }
19+
*/
20+
21+
func minDiffInBST(root *TreeNode) int {
22+
res, nodes := math.MaxInt16, -1
23+
dfsBST(root, &res, &nodes)
24+
return res
25+
}
26+
27+
func dfsBST(root *TreeNode, res, pre *int) {
28+
if root == nil {
29+
return
30+
}
31+
dfsBST(root.Left, res, pre)
32+
if *pre != -1 {
33+
*res = min(*res, abs(root.Val-*pre))
34+
}
35+
*pre = root.Val
36+
dfsBST(root.Right, res, pre)
37+
}
38+
39+
func min(a, b int) int {
40+
if a > b {
41+
return b
42+
}
43+
return a
44+
}
45+
46+
func abs(a int) int {
47+
if a > 0 {
48+
return a
49+
}
50+
return -a
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question783 struct {
11+
para783
12+
ans783
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para783 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans783 struct {
24+
one int
25+
}
26+
27+
func Test_Problem783(t *testing.T) {
28+
29+
qs := []question783{
30+
31+
{
32+
para783{[]int{4, 2, 6, 1, 3}},
33+
ans783{1},
34+
},
35+
36+
{
37+
para783{[]int{1, 0, 48, structures.NULL, structures.NULL, 12, 49}},
38+
ans783{1},
39+
},
40+
41+
{
42+
para783{[]int{90, 69, structures.NULL, 49, 89, structures.NULL, 52}},
43+
ans783{1},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 783------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans783, q.para783
51+
fmt.Printf("【input】:%v ", p)
52+
rootOne := structures.Ints2TreeNode(p.one)
53+
fmt.Printf("【output】:%v \n", minDiffInBST(rootOne))
54+
}
55+
fmt.Printf("\n\n\n")
56+
}

0 commit comments

Comments
 (0)