1
var getDirections = function (root, startValue, destValue) {
2
const getPath = (node, value, acc = "") => {
3
if (node === null) {
4
return "";
5
} else if (node.val === value) {
6
return acc;
7
} else {
8
return (
9
getPath(node.left, value, acc + "L") +
10
getPath(node.right, value, acc + "R")
11
);
12
}
13
};
14

15
// generate the paths
16
let startPath = getPath(root, startValue);
17
let destPath = getPath(root, destValue);
18

19
// find the lowest common ancestor
20
let i = 0;
21
for (
22
;
23
i < startPath.length && i < destPath.length && startPath[i] === destPath[i];
24
i++
25
);
26

27
// output the final path
28
let output = "";
29
for (let j = i; j < startPath.length; j++) {
30
output += "U";
31
}
32

33
return output + destPath.substring(i);
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0