-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
52 lines (52 loc) · 1.79 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
49
50
51
52
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> ansList=new ArrayList<String>();
if (root!=null) {
List<List<TreeNode>> totalList=new ArrayList<List<TreeNode>>();
List<TreeNode> initList=new ArrayList<TreeNode>();
initList.add(root);
totalList.add(initList);
for (int i = 0; i < totalList.size(); i++) {
List<TreeNode> list=totalList.get(i);
TreeNode node=list.get(list.size()-1);
boolean flag=false;
if (node.left!=null) {
List<TreeNode> leftList=new ArrayList<TreeNode>();
leftList.addAll(list);
leftList.add(node.left);
totalList.add(leftList);
flag=true;
}
if (node.right!=null) {
List<TreeNode> rightList=new ArrayList<TreeNode>();
rightList.addAll(list);
rightList.add(node.right);
totalList.add(rightList);
flag=true;
}
if (flag) {
totalList.remove(i);
i--;
}
}
for (int i = 0; i < totalList.size(); i++) {
List<TreeNode> list=totalList.get(i);
String ans=""+list.get(0).val;
for (int j = 1; j < list.size(); j++) {
ans+=("->"+list.get(j).val);
}
ansList.add(ans);
}
}
return ansList;
}
}