Skip to content

Commit 0ee16d7

Browse files
committed
https://leetcode.cn/problems/binary-tree-paths/
1 parent d15ace6 commit 0ee16d7

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/binary-tree-paths/
14+
1315
https://leetcode-cn.com/problems/trim-a-binary-search-tree/
1416

1517
https://leetcode.cn/problems/course-schedule/

binary-tree-paths/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
export default function binaryTreePaths(root: TreeNode | null): string[] {
4+
const res: string[] = [];
5+
if (!root) {
6+
return res;
7+
}
8+
const path: number[] = [];
9+
dfs(root, res, path);
10+
return res;
11+
}
12+
function dfs(root: TreeNode, res: string[], path: number[]) {
13+
path.push(root.val);
14+
if (!root.left && !root.right) {
15+
res.push(path.join("->"));
16+
return;
17+
}
18+
19+
for (const node of [root.left, root.right].filter(Boolean) as TreeNode[]) {
20+
dfs(node, res, path);
21+
path.pop();
22+
}
23+
}

0 commit comments

Comments
 (0)