MCPcopy Create free account
hub / github.com/OBKoro1/web-basics / binaryTreePaths

Function binaryTreePaths

src/leetCode/binary-tree-paths/binary-tree-paths.js:14–32  ·  view source on GitHub ↗
(root)

Source from the content-addressed store, hash-verified

12 * @return {string[]}
13 */
14var binaryTreePaths = function (root) {
15 let resArr = [] // 结果
16 const helperFn = (node, res) => {
17 if (node === null) return // 循环终止
18 // 添加当前节点到路径中
19 res += `${node.val}->`
20 // 叶子节点添加到最终结果中
21 if (node.left === null && node.right === null) {
22 res = res.substr(0, res.length - 2) // 去掉最后的->
23 resArr.push(res) // 添加该叶子节点
24 return
25 }
26 // 递归左右子节点
27 helperFn(node.left, res)
28 helperFn(node.right, res)
29 }
30 helperFn(root, '')
31 return resArr
32}
33
34// 广度优先
35// 两个栈 两个指针 每次推出同一个路径和树

Callers

nothing calls this directly

Calls 1

helperFnFunction · 0.85

Tested by

no test coverage detected