-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution257.java
More file actions
executable file
·45 lines (43 loc) · 1.27 KB
/
Copy pathSolution257.java
File metadata and controls
executable file
·45 lines (43 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
import java.util.*;
class Solution257 {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
List<String> ans = new ArrayList<>();
dfs(root, res, ans);
return res;
}
public void dfs(TreeNode p, List<String> res, List<String> ans) {
if (p != null && p.left == null && p.right == null) {
StringBuffer str = new StringBuffer();
for (int i = 0; i < ans.size(); i++)
str.append(ans.get(i) + "->");
str.append(String.valueOf(p.val));
res.add(str.toString());
return;
}
// 访问当前节点
ans.add(String.valueOf(p.val));
if (p.left != null) { // 遍历左节点
dfs(p.left, res, ans);
}
if (p.right != null) { // 遍历右节点
dfs(p.right, res, ans);
}
ans.remove(ans.size() - 1);
}
}