Skip to content

Commit f6c43c0

Browse files
committed
Update Solution.java
1 parent ecc2caa commit f6c43c0

File tree

1 file changed

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

1 file changed

+34
-48
lines changed
Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,41 @@
1-
/**
2-
* Definition for a binary tree node.
3-
* public class TreeNode {
4-
* int val;
5-
* TreeNode left;
6-
* TreeNode right;
7-
* TreeNode() {}
8-
* TreeNode(int val) { this.val = val; }
9-
* TreeNode(int val, TreeNode left, TreeNode right) {
10-
* this.val = val;
11-
* this.left = left;
12-
* this.right = right;
13-
* }
14-
* }
15-
*/
161
class Solution {
17-
static byte[] path = new byte[200_001];
18-
int strtLevel = -1;
19-
int destLevel = -1;
20-
int comnLevel = -1;
21-
222
public String getDirections(TreeNode root, int startValue, int destValue) {
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);
3+
TreeNode node = lca(root, startValue, destValue);
4+
StringBuilder pathToStart = new StringBuilder();
5+
StringBuilder pathToDest = new StringBuilder();
6+
dfs(node, startValue, pathToStart);
7+
dfs(node, destValue, pathToDest);
8+
return "U".repeat(pathToStart.length()) + pathToDest.toString();
289
}
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;
10+
11+
private TreeNode lca(TreeNode node, int p, int q) {
12+
if (node == null || node.val == p || node.val == q) {
13+
return node;
3914
}
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);
49-
}
15+
TreeNode left = lca(node.left, p, q);
16+
TreeNode right = lca(node.right, p, q);
17+
if (left != null && right != null) {
18+
return node;
5019
}
51-
if (comnLevel < 0 && leftFound + rightFound + result == 2)
52-
comnLevel = level;
53-
return result | leftFound | rightFound;
20+
return left != null ? left : right;
21+
}
22+
23+
private boolean dfs(TreeNode node, int x, StringBuilder path) {
24+
if (node == null) {
25+
return false;
26+
}
27+
if (node.val == x) {
28+
return true;
29+
}
30+
path.append('L');
31+
if (dfs(node.left, x, path)) {
32+
return true;
33+
}
34+
path.setCharAt(path.length() - 1, 'R');
35+
if (dfs(node.right, x, path)) {
36+
return true;
37+
}
38+
path.deleteCharAt(path.length() - 1);
39+
return false;
5440
}
5541
}

0 commit comments

Comments
 (0)