1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00

java: add «2096. Step-By-Step Directions From a Binary Tree Node to Another»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-07-16 23:29:42 +02:00
parent be27f896dc
commit 33e47203c6
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,70 @@
class Solution {
private TreeNode getLowestCommonAncestor(TreeNode node, int key1, int key2) {
if (node == null) {
return null;
}
if (node.val == key1 || node.val == key2) {
return node;
}
var left = getLowestCommonAncestor(node.left, key1, key2);
var right = getLowestCommonAncestor(node.right, key1, key2);
if (left == null) {
return right;
}
if (right == null) {
return left;
}
return node;
}
private record Path(boolean found, StringBuilder path) {}
private Path getPath(TreeNode node, int key, StringBuilder path) {
if (node == null) {
return new Path(false, path);
}
if (node.val == key) {
return new Path(true, path);
}
// go left
path.append('L');
var leftPath = getPath(node.left, key, path);
if (leftPath.found) {
return leftPath;
}
path.deleteCharAt(path.length() - 1);
// go right
path.append('R');
var rightPath = getPath(node.right, key, path);
if (rightPath.found) {
return rightPath;
}
path.deleteCharAt(path.length() - 1);
return new Path(false, path);
}
public String getDirections(TreeNode root, int startValue, int destValue) {
var commonAncestor = getLowestCommonAncestor(root, startValue, destValue);
var pathToStart = getPath(commonAncestor, startValue, new StringBuilder());
var pathToDest = getPath(commonAncestor, destValue, new StringBuilder());
var directions = new StringBuilder();
// gotta go up from the start
directions.repeat('U', pathToStart.path.length());
// and then follow the directions down
directions.append(pathToDest.path);
return directions.toString();
}
}