Solution1�� accepted
������java
class Solution {
public List
List
List
traverse(result, path, root);
return result;
}
private void traverse(List<String> result, List<Integer> path, TreeNode root) {
if (root == null) {
return;
}
path.add(root.val);
if (root.left == null && root.right == null) {
result.add(formatList(path));
} else {
traverse(result, path, root.left);
traverse(result, path, root.right);
}
path.remove(path.size() - 1);
}
private String formatList(List<Integer> path) {
StringBuilder sb = new StringBuilder();
sb.append(path.get(0));
for (int i = 1; i < path.size(); i++) {
sb.append("->" + path.get(i));
}
return sb.toString();
}
}
������
|
|