3
* @param {number[][]} edges5
* @param {number} target8
function dfs(n, map, t, target, visited, prop) {9
// edge case1: if run out of steps, cannot find target10
if (t === 0 && n !== target) return 0;11
// edge case2: if run out of steps, but found target12
if (t === 0 && n === target) return prop;15
// get unvisited children/neighbors16
const validChildren = [];17
(map[n] ?? []).forEach((child) => {18
if (!visited.has(child)) validChildren.push(child);20
// edge case3, if still more steps to use, but no more children to move,21
// if already at the targeted node, should just return22
if (n === target && t > 0 && !validChildren.length) return prop;23
// edge case4, if still more steps to use and no more children to move,24
// but current node is not target node, cannot find target, return 025
if (n !== target && t > 0 && !validChildren.length) return 0;27
// go to next valid child/neighbor28
for (let i = 0; i < validChildren.length; i++) {29
if (visited.has(validChildren[i])) continue;36
prop * (1 / validChildren.length)38
if (result !== 0) return result;44
var frogPosition = function (n, edges, t, target) {45
const map = new Array(n + 1);46
// make bidirectional edge map47
edges.forEach((item) => {48
if (!map[item[0]]) map[item[0]] = [];49
if (!map[item[1]]) map[item[1]] = [];50
map[item[0]].push(item[1]);51
map[item[1]].push(item[0]);54
return dfs(1, map, t, target, new Set(), 1);