class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList();
if(root==null) return result;
List<Integer> paths = new ArrayList();
dfs(root, paths, result);
return result;
}
private void dfs(TreeNode root, List<Integer> paths, List<String> result) {
paths.add(root.val);
if(root.left == null && root.right == null) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < paths.size()-1;i++) {
sb.append(paths.get(i)).append("->");
}
sb.append(paths.get(paths.size()-1));
result.add(sb.toString());
}
if(root.left != null) {
dfs(root.left, paths, result);
paths.remove(paths.size()-1);
}
if(root.right != null) {
dfs(root.right, paths, result);
paths.remove(paths.size()-1);
}
return;
}
}