forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePaths.java
More file actions
28 lines (25 loc) · 718 Bytes
/
BinaryTreePaths.java
File metadata and controls
28 lines (25 loc) · 718 Bytes
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
import java.util.LinkedList;
import java.util.List;
public class BinaryTreePaths {
// 耗时2ms
public List<String> binaryTreePaths(TreeNode root) {
List<String> list = new LinkedList<>();
if (root == null) {
return list;
}
helper(root, list, "");
return list;
}
private void helper(TreeNode root, List<String> list, String path) {
if (root == null) {
return;
}
path += (path.isEmpty() ? "" : "->") + root.val;
if (root.left == null && root.right == null) {
list.add(path);
return;
}
helper(root.left, list, path);
helper(root.right, list, path);
}
}