-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreePaths.java
53 lines (45 loc) · 1.04 KB
/
BinaryTreePaths.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
package easy;
import java.util.ArrayList;
import java.util.List;
/**
* Given a binary tree, return all root-to-leaf paths.
* For example, given the following binary tree:
*
* 1
* / \
* 2 3
* \
* 5
*
* All root-to-leaf paths are:
* ["1->2->5", "1->3"]
*
* 解决思路:
* DFS
* 每次递归在前一次基础上拼接结点值
*
* Created by second on 2017/7/17.
*/
public class BinaryTreePaths {
public List<String> binaryTreePaths(TreeNode root) {
List<String> list = new ArrayList<>();
path(root, "", list);
return list;
}
public void path(TreeNode root, String s, List<String> list){
if (root == null) return;
if (root.left == null && root.right == null){
list.add(s + root.val);
return;
}
s += root.val + "->";
path(root.left, s, list);
path(root.right, s, list);
}
private class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}