File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 105
105
- [ 217 Contains Duplicate] ( ./217 )
106
106
- [ 219 Contains Duplicate II] ( ./219 )
107
107
- [ 238 Product of Array Except Self] ( ./238 )
108
+ - [ 257 Binary Tree Paths] ( ./257 )
108
109
- [ 270 Closest Binary Search Tree Value] ( ./270 )
109
110
- [ 278 First Bad Version] ( ./278 )
110
111
- [ 283 Move Zeroes] ( ./283 )
You can’t perform that action at this time.
0 commit comments