/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: the root of the binary tree
* @return: all root-to-leaf paths
*/
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root == null) return result;
dfs(root, String.valueOf(root.val), result);
return result;
}
private void dfs(TreeNode root, String path, List<String> result) {
if (root == null) return;
if (root.left == null && root.right == null)
result.add(path);
if (root.left != null)
dfs(root.left, path + "->" + root.left.val, result);
if (root.right != null)
dfs(root.right, path + "->" + root.right.val, result);
}
}