Skip to content

Commit 8abb3a0

Browse files
authored
Improved task 530
1 parent 8548bba commit 8abb3a0

File tree

2 files changed

+33
-58
lines changed

2 files changed

+33
-58
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
11851185
|-|-|-|-|-|-
11861186
| 0215 |[Kth Largest Element in an Array](src/main/go/g0201_0300/s0215_kth_largest_element_in_an_array/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, LeetCode_75_Heap/Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 17 | 83.49
11871187
| 0502 |[IPO](src/main/go/g0501_0600/s0502_ipo/solution.go)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 62 | 91.95
1188-
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/priorrity_queue.go)| |||
1188+
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.go)| Medium | Array, Heap_Priority_Queue | 21 | 90.40
11891189
| 0295 |[Find Median from Data Stream](src/main/go/g0201_0300/s0295_find_median_from_data_stream/medianfinder.go)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 59 | 96.70
11901190

11911191
#### Top Interview 150 Bit Manipulation
@@ -1782,7 +1782,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
17821782
| 0392 |[Is Subsequence](src/main/go/g0301_0400/s0392_is_subsequence/solution.go)| Easy | String, Dynamic_Programming, Two_Pointers, LeetCode_75_Two_Pointers, Dynamic_Programming_I_Day_19, Level_1_Day_2_String, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers | 0 | 100.00
17831783
| 0383 |[Ransom Note](src/main/go/g0301_0400/s0383_ransom_note/solution.go)| Easy | String, Hash_Table, Counting, Data_Structure_I_Day_6_String, Top_Interview_150_Hashmap | 0 | 100.00
17841784
| 0380 |[Insert Delete GetRandom O(1)](src/main/go/g0301_0400/s0380_insert_delete_getrandom_o1/randomizedset.go)| Medium | Array, Hash_Table, Math, Design, Randomized, Programming_Skills_II_Day_20, Top_Interview_150_Array/String | 47 | 87.65
1785-
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/priorrity_queue.go)| |||
1785+
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.go)| Medium | Array, Heap_Priority_Queue, Top_Interview_150_Heap | 21 | 90.40
17861786
| 0347 |[Top K Frequent Elements](src/main/go/g0301_0400/s0347_top_k_frequent_elements/solution.go)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 0 | 100.00
17871787
| 0338 |[Counting Bits](src/main/go/g0301_0400/s0338_counting_bits/solution.go)| Easy | Dynamic_Programming, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 0 | 100.00
17881788
| 0322 |[Coin Change](src/main/go/g0301_0400/s0322_coin_change/solution.go)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(m\*n)_Space_O(amount) | 10 | 79.59

src/main/go/g0501_0600/s0530_minimum_absolute_difference_in_bst/solution_test.go

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,39 @@ import (
55
"testing"
66
)
77

8-
func TestGetMinimumDifference(t *testing.T) {
9-
// Test case 1: [4,2,6,1,3]
10-
root1 := &TreeNode{
11-
Val: 4,
12-
Left: &TreeNode{
13-
Val: 2,
14-
Left: &TreeNode{
15-
Val: 1,
16-
},
17-
Right: &TreeNode{
18-
Val: 3,
19-
},
20-
},
21-
Right: &TreeNode{
22-
Val: 6,
23-
},
8+
// Constructs a binary tree from a level-order slice where nil means no node
9+
func constructBinaryTree(vals []any) *TreeNode {
10+
if len(vals) == 0 || vals[0] == nil {
11+
return nil
2412
}
25-
assert.Equal(t, 1, getMinimumDifference(root1))
26-
27-
// Test case 2: [1,0,48,null,null,12,49]
28-
root2 := &TreeNode{
29-
Val: 1,
30-
Left: &TreeNode{
31-
Val: 0,
32-
},
33-
Right: &TreeNode{
34-
Val: 48,
35-
Left: &TreeNode{
36-
Val: 12,
37-
},
38-
Right: &TreeNode{
39-
Val: 49,
40-
},
41-
},
13+
nodes := make([]*TreeNode, len(vals))
14+
for i, v := range vals {
15+
if v != nil {
16+
nodes[i] = &TreeNode{Val: v.(int)}
17+
}
4218
}
43-
assert.Equal(t, 1, getMinimumDifference(root2))
44-
45-
// Test case 3: Single node
46-
root3 := &TreeNode{
47-
Val: 1,
19+
child := 1
20+
for i := range nodes {
21+
if nodes[i] != nil && child < len(vals) {
22+
if child < len(nodes) && nodes[child] != nil {
23+
nodes[i].Left = nodes[child]
24+
}
25+
child++
26+
if child < len(nodes) && nodes[child] != nil {
27+
nodes[i].Right = nodes[child]
28+
}
29+
child++
30+
}
4831
}
49-
assert.Equal(t, 1<<31-1, getMinimumDifference(root3))
32+
return nodes[0]
33+
}
5034

51-
// Test case 4: [236,104,701,null,227,null,911]
52-
root4 := &TreeNode{
53-
Val: 236,
54-
Left: &TreeNode{
55-
Val: 104,
56-
Right: &TreeNode{
57-
Val: 227,
58-
},
59-
},
60-
Right: &TreeNode{
61-
Val: 701,
62-
Right: &TreeNode{
63-
Val: 911,
64-
},
65-
},
66-
}
67-
assert.Equal(t, 9, getMinimumDifference(root4))
35+
func TestGetMinimumDifference(t *testing.T) {
36+
tree := constructBinaryTree([]any{4, 2, 6, 1, 3})
37+
assert.Equal(t, 1, getMinimumDifference(tree))
38+
}
39+
40+
func TestGetMinimumDifference2(t *testing.T) {
41+
tree := constructBinaryTree([]any{1, 0, 48, nil, nil, 12, 49})
42+
assert.Equal(t, 1, getMinimumDifference(tree))
6843
}

0 commit comments

Comments
 (0)