Skip to content

Commit 5795135

Browse files
committed
257
1 parent 9bc9e35 commit 5795135

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

257/Solution.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<String> binaryTreePaths(TreeNode root) {
3+
List<String> list = new LinkedList<>();
4+
if (root == null) return list;
5+
dfs(root, "", list);
6+
return list;
7+
}
8+
9+
private void dfs(TreeNode root, String path, List<String> list) {
10+
if (root.left == null && root.right == null) {
11+
list.add(path + root.val);
12+
return;
13+
}
14+
if (root.left != null) dfs(root.left, path + root.val + "->", list);
15+
if (root.right != null) dfs(root.right, path + root.val + "->", list);
16+
}
17+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
- [217 Contains Duplicate](./217)
106106
- [219 Contains Duplicate II](./219)
107107
- [238 Product of Array Except Self](./238)
108+
- [257 Binary Tree Paths](./257)
108109
- [270 Closest Binary Search Tree Value](./270)
109110
- [278 First Bad Version](./278)
110111
- [283 Move Zeroes](./283)

0 commit comments

Comments
 (0)