File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments