Skip to content

Commit 52c6f61

Browse files
authored
Added tasks 783, 784.
1 parent 0c9a6f7 commit 52c6f61

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0701_0800.s0783_minimum_distance_between_bst_nodes;
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
4+
5+
import com_github_leetcode.TreeNode;
6+
7+
/*
8+
* Definition for a binary tree node.
9+
* public class TreeNode {
10+
* int val;
11+
* TreeNode left;
12+
* TreeNode right;
13+
* TreeNode() {}
14+
* TreeNode(int val) { this.val = val; }
15+
* TreeNode(int val, TreeNode left, TreeNode right) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
public class Solution {
23+
private int prev = -1;
24+
private int min = Integer.MAX_VALUE;
25+
26+
public int minDiffInBST(TreeNode root) {
27+
if (root == null) {
28+
return min;
29+
}
30+
minDiffInBST(root.left);
31+
if (prev == -1) {
32+
prev = root.val;
33+
} else {
34+
min = Math.min(min, Math.abs(root.val - prev));
35+
prev = root.val;
36+
}
37+
minDiffInBST(root.right);
38+
return min;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
783\. Minimum Distance Between BST Nodes
2+
3+
Easy
4+
5+
Given the `root` of a Binary Search Tree (BST), return _the minimum difference between the values of any two different nodes in the tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg)
10+
11+
**Input:** root = [4,2,6,1,3]
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg)
18+
19+
**Input:** root = [1,0,48,null,null,12,49]
20+
21+
**Output:** 1
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range `[2, 100]`.
26+
* <code>0 <= Node.val <= 10<sup>5</sup></code>
27+
28+
**Note:** This question is the same as 530: [https://leetcode.com/problems/minimum-absolute-difference-in-bst/](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0701_0800.s0784_letter_case_permutation;
2+
3+
// #Medium #String #Bit_Manipulation #Backtracking
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
private List<String> ans = new ArrayList<>();
10+
11+
public List<String> letterCasePermutation(String s) {
12+
helper(s, 0, "");
13+
return ans;
14+
}
15+
16+
public void helper(String s, int curr, String temp) {
17+
if (curr == s.length()) {
18+
ans.add(temp);
19+
return;
20+
}
21+
if (Character.isDigit(s.charAt(curr))) {
22+
helper(s, curr + 1, temp + s.substring(curr, curr + 1));
23+
} else {
24+
if (Character.isLowerCase(s.charAt(curr))) {
25+
helper(s, curr + 1, temp + s.substring(curr, curr + 1));
26+
helper(s, curr + 1, temp + (s.substring(curr, curr + 1)).toUpperCase());
27+
} else {
28+
helper(s, curr + 1, temp + s.substring(curr, curr + 1));
29+
helper(s, curr + 1, temp + (s.substring(curr, curr + 1)).toLowerCase());
30+
}
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
784\. Letter Case Permutation
2+
3+
Medium
4+
5+
Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
6+
7+
Return _a list of all possible strings we could create_. Return the output in **any order**.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "a1b2"
12+
13+
**Output:** ["a1b2","a1B2","A1b2","A1B2"]
14+
15+
**Example 2:**
16+
17+
**Input:** s = "3z4"
18+
19+
**Output:** ["3z4","3Z4"]
20+
21+
**Constraints:**
22+
23+
* `1 <= s.length <= 12`
24+
* `s` consists of lowercase English letters, uppercase English letters, and digits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0701_0800.s0783_minimum_distance_between_bst_nodes;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeUtils;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void minDiffInBST() {
13+
assertThat(
14+
new Solution()
15+
.minDiffInBST(
16+
TreeUtils.constructBinaryTree(
17+
Arrays.asList(4, 2, 6, 1, 3, null, null))),
18+
equalTo(1));
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0701_0800.s0784_letter_case_permutation;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void letterCasePermutation() {
12+
assertThat(
13+
new Solution().letterCasePermutation("a1b2"),
14+
equalTo(Arrays.asList("a1b2", "a1B2", "A1b2", "A1B2")));
15+
}
16+
17+
@Test
18+
void letterCasePermutation2() {
19+
assertThat(
20+
new Solution().letterCasePermutation("3z4"), equalTo(Arrays.asList("3z4", "3Z4")));
21+
}
22+
}

0 commit comments

Comments
 (0)