|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.TreeNode;
|
4 |
| -import com.fishercoder.common.utils.CommonUtils; |
5 | 4 |
|
6 | 5 | import java.util.ArrayList;
|
7 | 6 | import java.util.List;
|
|
23 | 22 | */
|
24 | 23 |
|
25 | 24 | public class _257 {
|
26 |
| - //a very typical/good question to test your recursion/dfs understanding. |
27 |
| - public List<String> binaryTreePaths_more_concise(TreeNode root) { |
28 |
| - List<String> paths = new ArrayList<String>(); |
29 |
| - if(root == null) return paths; |
30 |
| - dfs(root, paths, ""); |
31 |
| - return paths; |
32 |
| - } |
33 |
| - |
34 |
| - private void dfs(TreeNode root, List<String> paths, String path) { |
35 |
| - if(root.left == null && root.right == null){ |
36 |
| - paths.add(path + root.val); |
37 |
| - return; |
| 25 | + public static class Solution1 { |
| 26 | + //a very typical/good question to test your recursion/dfs understanding. |
| 27 | + public List<String> binaryTreePaths_more_concise(TreeNode root) { |
| 28 | + List<String> paths = new ArrayList<String>(); |
| 29 | + if (root == null) return paths; |
| 30 | + dfs(root, paths, ""); |
| 31 | + return paths; |
38 | 32 | }
|
39 |
| - path += root.val + "->"; |
40 |
| - if(root.left != null) dfs(root.left, paths, path); |
41 |
| - if(root.right != null) dfs(root.right, paths, path); |
42 |
| - } |
43 |
| - |
44 |
| - public static void main(String...strings){ |
45 |
| - _257 test = new _257(); |
46 |
| - TreeNode root = new TreeNode(1); |
47 |
| - root.left = new TreeNode(2); |
48 |
| - root.left.right = new TreeNode(5); |
49 |
| - root.right = new TreeNode(3); |
50 |
| - List<String> res = test.binaryTreePaths(root); |
51 |
| - CommonUtils.print(res); |
52 |
| - } |
53 | 33 |
|
54 |
| - public List<String> binaryTreePaths(TreeNode root) { |
55 |
| - List<String> paths = new ArrayList<String>(); |
56 |
| - dfs(root, paths, new StringBuilder()); |
57 |
| - return paths; |
| 34 | + private void dfs(TreeNode root, List<String> paths, String path) { |
| 35 | + if (root.left == null && root.right == null) { |
| 36 | + paths.add(path + root.val); |
| 37 | + return; |
| 38 | + } |
| 39 | + path += root.val + "->"; |
| 40 | + if (root.left != null) dfs(root.left, paths, path); |
| 41 | + if (root.right != null) dfs(root.right, paths, path); |
| 42 | + } |
58 | 43 | }
|
| 44 | + |
| 45 | + public static class Solution2 { |
| 46 | + public List<String> binaryTreePaths(TreeNode root) { |
| 47 | + List<String> paths = new ArrayList<String>(); |
| 48 | + dfs(root, paths, new StringBuilder()); |
| 49 | + return paths; |
| 50 | + } |
59 | 51 |
|
60 |
| - private void dfs(TreeNode root, List<String> paths, StringBuilder sb) { |
61 |
| - if(root == null) return; |
62 |
| - if(root.left == null && root.right == null){ |
63 |
| - sb.append(root.val); |
64 |
| - paths.add(sb.toString()); |
65 |
| - return ; |
66 |
| - } |
67 |
| - sb.append(root.val + "->"); |
68 |
| - String curr = sb.toString(); |
69 |
| - if(root.left != null) dfs(root.left, paths, sb); |
70 |
| - sb.setLength(0); |
71 |
| - sb.append(curr); |
72 |
| - if(root.right != null) dfs(root.right, paths, sb); |
| 52 | + private void dfs(TreeNode root, List<String> paths, StringBuilder sb) { |
| 53 | + if (root == null) return; |
| 54 | + if (root.left == null && root.right == null) { |
| 55 | + sb.append(root.val); |
| 56 | + paths.add(sb.toString()); |
| 57 | + return; |
| 58 | + } |
| 59 | + sb.append(root.val + "->"); |
| 60 | + String curr = sb.toString(); |
| 61 | + if (root.left != null) dfs(root.left, paths, sb); |
| 62 | + sb.setLength(0); |
| 63 | + sb.append(curr); |
| 64 | + if (root.right != null) dfs(root.right, paths, sb); |
| 65 | + } |
73 | 66 | }
|
74 | 67 | }
|
0 commit comments