// const _ = require('lodash');// function sayHello() {// console.log('Hello, World');// }// _.times(5, sayHello);// 有一棵二叉树,二叉树中有两个节点s和t,输出从s到t的最短有向路径。其中,‘U’代表向上,‘L’代表向左,‘R’代表向右// Example:// 1// / \// 2 3// / / \// 4 5 6// s = 4, t = 5, output: "UU RL" (4->2->1->3->5)// s = 4, t = 6, U Rfunction test(root, s, t) { let result = []; // 面试官问递归的返回是什么,怎么考虑 function walk(node, path) { if (node === null || node === undefined) { return; } if (node.val === s.val || node.val === t.val) { result.push(path); return true; } let r1= walk(node.left, [...path, 'L']); let r2 = walk(node.right, [...path, 'R']); if (r1 && r2) { let n = path.length; // 减去公共的首部 result[0] = result.slice(n); result[1] = result.slice(n); } } walk(root, []); console.log(result); if (result.length === 2) { let tmp = result[0].map(() => 'U'); return [...tmp, ...result[1]].join(''); } if (result.length === 1) { let tmp = result[0].map(() => 'U'); return tmp.join(''); } return result;}function Node(val, left, right) { this.val = val; this.left = left; this.right = right;}// 1// / \// 2 3// / / \// 4 5 6const n1 = new Node(1);const n2 = new Node(2);const n3 = new Node(3);const n4 = new Node(4);const n5 = new Node(5);const n6 = new Node(6);n1.left = n2;n1.right = n3;n2.left = n4;n3.left = n5;n3.right = n6;// console.log(test(n1, n4, n2));console.log(test(n1, n5, n6));