-
Notifications
You must be signed in to change notification settings - Fork 457
/
Solution.java
48 lines (39 loc) · 1.3 KB
/
Solution.java
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
46
47
48
import java.util.List;
import java.util.ArrayList;
/// 257. Binary Tree Paths
/// https://leetcode.com/problems/binary-tree-paths/description/
/// 时间复杂度: O(n), n为树中的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution {
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public List<String> binaryTreePaths(TreeNode root) {
ArrayList<String> res = new ArrayList<String>();
if(root == null)
return res;
if(root.left == null && root.right == null){
res.add(Integer.toString(root.val));
return res;
}
List<String> leftPaths = binaryTreePaths(root.left);
for(String s: leftPaths){
StringBuilder sb = new StringBuilder(Integer.toString(root.val));
sb.append("->");
sb.append(s);
res.add(sb.toString());
}
List<String> rightPaths = binaryTreePaths(root.right);
for(String s: rightPaths) {
StringBuilder sb = new StringBuilder(Integer.toString(root.val));
sb.append("->");
sb.append(s);
res.add(sb.toString());
}
return res;
}
}