Skip to content

Commit e0f70f2

Browse files
committedJul 16, 2024·
add 2096
1 parent 96e133d commit e0f70f2

File tree

3 files changed

+113
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+113
-0
lines changed
 

‎paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
| 2108 | [Find First Palindromic String in the Array](https://leetcode.com/problems/find-first-palindromic-string-in-the-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2108.java) || Easy ||
152152
| 2103 | [Rings and Rods](https://leetcode.com/problems/rings-and-rods/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2103.java) || Easy ||
153153
| 2099 | [Find Subsequence of Length K With the Largest Sum](https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2099.java) || Easy ||
154+
| 2096 | [Step-By-Step Directions From a Binary Tree Node to Another](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2096.java) || Medium |DFS, Tree
154155
| 2095 | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2095.java) || Medium ||
155156
| 2094 | [Finding 3-Digit Even Numbers](https://leetcode.com/problems/finding-3-digit-even-numbers/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2094.java) || Easy ||
156157
| 2091 | [Removing Minimum and Maximum From Array](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2091.java) || Medium ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
public class _2096 {
6+
public static class Solution1 {
7+
/**
8+
* Steps for this problem:
9+
* 1. find the path from root the start and dest respectively, mark them using two directions: 'L' and 'R', i.e. you can only go down from root, so there's no up, 'U' direction;
10+
* 2. the LCA (the lowest common ancestor) of start and dest will be the joint of the shortest path;
11+
* 3. find the longest common prefix of these two paths, that can be cut off;
12+
* 4. reverse the startPath, so it becomes the path from start to LCA, then concatenate with destPath
13+
*/
14+
public String getDirections(TreeNode root, int startValue, int destValue) {
15+
StringBuilder sb = new StringBuilder();
16+
String pathForStart = "";
17+
if (findPathFromRoot(root, startValue, sb)) {
18+
pathForStart = sb.toString();
19+
}
20+
sb.setLength(0);
21+
String pathForDest = "";
22+
if (findPathFromRoot(root, destValue, sb)) {
23+
pathForDest = sb.toString();
24+
}
25+
int lastIdenticalCharIndex = -1;
26+
int minLen = Math.min(pathForStart.length(), pathForDest.length());
27+
for (int i = 0; i < minLen; i++) {
28+
if (pathForStart.charAt(i) == pathForDest.charAt(i)) {
29+
lastIdenticalCharIndex = i;
30+
} else {
31+
break;
32+
}
33+
}
34+
sb.setLength(0);
35+
sb.append(pathForStart.substring(lastIdenticalCharIndex + 1));
36+
for (int i = 0; i < sb.length(); i++) {
37+
if (sb.charAt(i) == 'L' || sb.charAt(i) == 'R') {
38+
sb.setCharAt(i, 'U');
39+
}
40+
}
41+
sb.append(pathForDest.substring(lastIdenticalCharIndex + 1));
42+
return sb.toString();
43+
}
44+
45+
private boolean findPathFromRoot(TreeNode root, int target, StringBuilder sb) {
46+
if (root == null) {
47+
return false;
48+
}
49+
if (root.val == target) {
50+
return true;
51+
}
52+
if (findPathFromRoot(root.left, target, sb.append("L"))) {
53+
return true;
54+
}
55+
sb.setLength(sb.length() - 1);
56+
if (findPathFromRoot(root.right, target, sb.append("R"))) {
57+
return true;
58+
}
59+
sb.setLength(sb.length() - 1);
60+
return false;
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions.thirdthousand._2096;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class _2096Test {
14+
private static _2096.Solution1 solution1;
15+
16+
@BeforeEach
17+
public void setup() {
18+
solution1 = new _2096.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(5, 1, 2, 3, null, 6, 4));
24+
TreeUtils.printBinaryTree(root);
25+
assertEquals("UURL", solution1.getDirections(root, 3, 6));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(2, 1));
31+
TreeUtils.printBinaryTree(root);
32+
assertEquals("L", solution1.getDirections(root, 2, 1));
33+
}
34+
35+
@Test
36+
public void test3() {
37+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2));
38+
TreeUtils.printBinaryTree(root);
39+
assertEquals("U", solution1.getDirections(root, 2, 1));
40+
}
41+
42+
@Test
43+
public void test4() {
44+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(5, 8, 3, 1, null, 4, 7, 6, null, null, null, null, null, null, 2));
45+
TreeUtils.printBinaryTree(root);
46+
assertEquals("U", solution1.getDirections(root, 4, 3));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)
Please sign in to comment.