var getDirections = function(root, startValue, destValue) { const startPath = []; const destPath = []; find(root, startValue, startPath); find(root, destValue, destPath); // find the root while (startPath.length && destPath.length && startPath.at(-1) === destPath.at(-1)) { startPath.pop(); destPath.pop(); } // L -> U return startPath.map(() => "U").join("") + destPath.reverse().join(""); }; function find(root, d, path) { if(!root) return false; if(root.val === d) return true; if(root.left && find(root.left, d, path)) { path.push("L") return true } if(root.right && find(root.right, d, path)) { path.push("R") return true } return false }
LeetCode 2096. Пошаговые инструкции от одного узла бинарного дерева к другому — JavaScript
schedule
13.10.2023