-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLeet257.java
More file actions
83 lines (76 loc) · 2.55 KB
/
Leet257.java
File metadata and controls
83 lines (76 loc) · 2.55 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* 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> list = new ArrayList<>();
helper(list,root,"");
return list;
}
public void helper(List<String> list,TreeNode node,String path){
if(node != null){
if(node.left == null && node.right == null){
list.add(path+node.val);
} else {
helper(list,node.left,path+node.val+"->");
helper(list,node.right,path+node.val+"->");
}
}
}
}
//写乱了
/***
public List<String> binaryTreePaths(TreeNode root) {
//Key point:similar to Permutations
//因为是一直向下的,不用回头。用Permutations方法反而麻烦了.....
List<String> list = new ArrayList();
List<Integer> item = new ArrayList();
//Key point:记得区分出right和left,否则会重复加一遍path
helper(list,item,root);
return list;
}
public void helper(List<String> list,List<Integer> item,TreeNode node){
//Key point:写法有问题,如果left为null,但right不为null,那么也会加上。不符题意
/**
if(node == null){
if(right == 0){
String tmp = "";
for(int i = 0;i <= item.size()-1;i++){
if(i != 0) tmp += "->"+item.get(i);
else tmp += item.get(i)+"";
}
list.add(tmp);
}
//Key point:像这种递归没有return的,还是加上else吧
} else {
item.add(node.val);
helper(list,item,node.left,0);
helper(list,item,node.right,1);
item.remove(item.size()-1);
}
if(node != null){
if(node.right == null && node.left == null){
String tmp = "";
for(int i = 0;i <= item.size()-1;i++){
if(i != 0) tmp += "->"+item.get(i);
else tmp += item.get(i)+"";
}
//Key point:因为叶节点未经过递归,所以要手动加一下
//list.add(tmp);
if(item.size() == 0) list.add(tmp+node.val);
else list.add(tmp+"->"+node.val);
} else {
item.add(node.val);
helper(list,item,node.left);
helper(list,item,node.right);
item.remove(item.size()-1);
}
}
}
***/