题目描述
代码
public class Solution {
public List<String> binaryTreePaths(TreeNode root){
String path = new String();
List<String> paths = new ArrayList<>();
construct_paths(root,path,paths);
return paths;
}
public void construct_paths(TreeNode root,String path,List<String> paths){
//不需要返回值,利用引用对对象进行修改
if(root!=null){
path+=Integer.toString(root.val);
if(root.left == null && root.right == null){
paths.add(path);
}else{
path+="->";
construct_paths(root.left,path,paths);
construct_paths(root.right,path,paths);
}
}
}
}
性能表现