Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 4x 4x 10x 5x 5x 5x 3x 5x 5x 4x 1x 3x 3x | import type { TreeNode } from './Tree'
export default function binaryTreePaths(
root: TreeNode<number> | null,
): string[] {
const paths: string[] = []
const dfs = (root: TreeNode<number>, path: string) => {
if (root.left === null && root.right === null) {
paths.push(path)
return
}
if (root.left)
dfs(root.left, `${path}->${root.left.val.toString()}`)
if (root.right)
dfs(root.right, `${path}->${root.right.val.toString()}`)
}
if (root === null)
return paths
dfs(root, root.val.toString())
return paths
}
|