https://leetcode.com/problems/binary-tree-paths/
找出二叉树根节点到叶子节点的所有路径
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new LinkedList();
if (root != null) {
search(root, res, "");
}
return res;
}
private void search(TreeNode root, List<String> res, String part) {
if (root.left == null && root.right == null) {
res.add(part + root.val);
}
if (root.left != null) {
search(root.left, res, part + root.val + "->");
}
if (root.right != null) {
search(root.right, res, part + root.val + "->");
}
}
}