题目:

解析:

代码:
private List<String> ret = new ArrayList<>();
public List<String> binaryTreePaths(TreeNode root) {
dfs(root,new StringBuffer());
return ret;
}
private void dfs(TreeNode root, StringBuffer no_path){
//把path设置为局部变量,让函数帮我们 “恢复现场”
StringBuffer path = new StringBuffer(no_path);
path.append(Integer.toString(root.val));
//叶子节点
if(root.left == null && root.right == null){
ret.add(path.toString());
return;
}
//非叶子节点
path.append("->");
//加上剪枝写法
if(root.left != null) dfs(root.left,path);
if(root.right != null) dfs(root.right,path);
}