File tree 1 file changed +26
-1
lines changed
1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -390,6 +390,8 @@ public:
390
390
391
391
```Java
392
392
//解法一
393
+
394
+ //方式一
393
395
class Solution {
394
396
/**
395
397
* 递归法
@@ -428,9 +430,32 @@ class Solution {
428
430
}
429
431
}
430
432
}
433
+
434
+ //方式二
435
+ class Solution {
436
+
437
+ List<String> result = new ArrayList<>();
438
+
439
+ public List<String> binaryTreePaths(TreeNode root) {
440
+ deal(root, "");
441
+ return result;
442
+ }
443
+
444
+ public void deal(TreeNode node, String s) {
445
+ if (node == null)
446
+ return;
447
+ if (node.left == null && node.right == null) {
448
+ result.add(new StringBuilder(s).append(node.val).toString());
449
+ return;
450
+ }
451
+ String tmp = new StringBuilder(s).append(node.val).append("->").toString();
452
+ deal(node.left, tmp);
453
+ deal(node.right, tmp);
454
+ }
455
+ }
431
456
```
432
457
``` java
433
- // 解法2
458
+ // 解法二
434
459
class Solution {
435
460
/**
436
461
* 迭代法
You can’t perform that action at this time.
0 commit comments