Skip to content

Commit 48063f2

Browse files
dhan-profileyanglbme
authored andcommitted
Optimized Solution
1 parent ee2f5e3 commit 48063f2

File tree

1 file changed

+33
-47
lines changed
  • solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another

1 file changed

+33
-47
lines changed

solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.java

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,42 @@
1414
* }
1515
*/
1616
class Solution {
17-
private Map<Integer, List<List<String>>> edges;
18-
private Set<Integer> visited;
19-
private String ans;
20-
17+
static byte[] path = new byte[200_001];
18+
int strtLevel = -1;
19+
int destLevel = -1;
20+
int comnLevel = -1;
21+
2122
public String getDirections(TreeNode root, int startValue, int destValue) {
22-
edges = new HashMap<>();
23-
visited = new HashSet<>();
24-
ans = null;
25-
traverse(root);
26-
dfs(startValue, destValue, new ArrayList<>());
27-
return ans;
23+
findPaths(root, startValue, destValue, 100_000);
24+
int answerIdx = comnLevel;
25+
for (int i = strtLevel; i > comnLevel; i--)
26+
path[--answerIdx] = 'U';
27+
return new String(path, answerIdx, destLevel - answerIdx);
2828
}
29-
30-
private void traverse(TreeNode root) {
31-
if (root == null) {
32-
return;
29+
30+
private int findPaths(TreeNode node, int strtVal, int destVal, int level) {
31+
if (node == null) return 0;
32+
int result = 0;
33+
if (node.val == strtVal) {
34+
strtLevel = level;
35+
result = 1;
36+
} else if (node.val == destVal) {
37+
destLevel = level;
38+
result = 1;
3339
}
34-
if (root.left != null) {
35-
edges.computeIfAbsent(root.val, k -> new ArrayList<>())
36-
.add(Arrays.asList(String.valueOf(root.left.val), "L"));
37-
edges.computeIfAbsent(root.left.val, k -> new ArrayList<>())
38-
.add(Arrays.asList(String.valueOf(root.val), "U"));
39-
}
40-
if (root.right != null) {
41-
edges.computeIfAbsent(root.val, k -> new ArrayList<>())
42-
.add(Arrays.asList(String.valueOf(root.right.val), "R"));
43-
edges.computeIfAbsent(root.right.val, k -> new ArrayList<>())
44-
.add(Arrays.asList(String.valueOf(root.val), "U"));
45-
}
46-
traverse(root.left);
47-
traverse(root.right);
48-
}
49-
50-
private void dfs(int start, int dest, List<String> t) {
51-
if (visited.contains(start)) {
52-
return;
53-
}
54-
if (start == dest) {
55-
if (ans == null || ans.length() > t.size()) {
56-
ans = String.join("", t);
57-
}
58-
return;
59-
}
60-
visited.add(start);
61-
if (edges.containsKey(start)) {
62-
for (List<String> item : edges.get(start)) {
63-
t.add(item.get(1));
64-
dfs(Integer.parseInt(item.get(0)), dest, t);
65-
t.remove(t.size() - 1);
40+
int leftFound = 0;
41+
int rightFound = 0;
42+
if (comnLevel < 0) {
43+
if (destLevel < 0) path[level] = 'L';
44+
leftFound = findPaths(node.left, strtVal, destVal, level + 1);
45+
rightFound = 0;
46+
if (comnLevel < 0) {
47+
if (destLevel < 0) path[level] = 'R';
48+
rightFound = findPaths(node.right, strtVal, destVal, level + 1);
6649
}
6750
}
51+
if (comnLevel < 0 && leftFound + rightFound + result == 2)
52+
comnLevel = level;
53+
return result | leftFound | rightFound;
6854
}
69-
}
55+
}

0 commit comments

Comments
 (0)