(root)
| 12 | * @return {string[]} |
| 13 | */ |
| 14 | var 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 | // 两个栈 两个指针 每次推出同一个路径和树 |
nothing calls this directly
no test coverage detected