Skip to content

Commit f22c658

Browse files
authored
Update 0257.二叉树的所有路径.md
1 parent 842bafc commit f22c658

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

problems/0257.二叉树的所有路径.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ public:
390390
391391
```Java
392392
//解法一
393+
394+
//方式一
393395
class Solution {
394396
/**
395397
* 递归法
@@ -428,9 +430,32 @@ class Solution {
428430
}
429431
}
430432
}
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+
}
431456
```
432457
```java
433-
// 解法2
458+
// 解法二
434459
class Solution {
435460
/**
436461
* 迭代法

0 commit comments

Comments
 (0)