forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_257.java
63 lines (57 loc) · 1.88 KB
/
_257.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
53
54
55
56
57
58
59
60
61
62
63
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _257 {
public static class Solution1 {
//a very typical/good question to test your recursion/dfs understanding.
public List<String> binaryTreePaths_more_concise(TreeNode root) {
List<String> paths = new ArrayList<>();
if (root == null) {
return paths;
}
dfs(root, paths, "");
return paths;
}
private void dfs(TreeNode root, List<String> paths, String path) {
if (root.left == null && root.right == null) {
paths.add(path + root.val);
return;
}
path += root.val + "->";
if (root.left != null) {
dfs(root.left, paths, path);
}
if (root.right != null) {
dfs(root.right, paths, path);
}
}
}
public static class Solution2 {
public List<String> binaryTreePaths(TreeNode root) {
List<String> paths = new ArrayList<>();
dfs(root, paths, new StringBuilder());
return paths;
}
private void dfs(TreeNode root, List<String> paths, StringBuilder sb) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
sb.append(root.val);
paths.add(sb.toString());
return;
}
sb.append(root.val + "->");
String curr = sb.toString();
if (root.left != null) {
dfs(root.left, paths, sb);
}
sb.setLength(0);
sb.append(curr);
if (root.right != null) {
dfs(root.right, paths, sb);
}
}
}
}