Skip to content

Commit 559b297

Browse files
committed
Time: 8 ms (41.69%) | Memory: 42.7 MB (26.22%) - LeetSync
1 parent e112ae4 commit 559b297

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public List<String> binaryTreePaths(TreeNode root) {
18+
19+
List<String> list = new ArrayList<>();
20+
dfs(root , "" , list);
21+
return list;
22+
}
23+
void dfs(TreeNode node , String path , List<String> list){
24+
25+
if(node==null)return;
26+
27+
path += node.val;
28+
29+
if(node.left==null && node.right==null){
30+
list.add(path);
31+
}else{
32+
path +="->";
33+
dfs(node.left , path , list);
34+
dfs(node.right , path , list);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)