257.递归输出二叉树的所有路径

binary-tree-paths

题目描述
在这里插入图片描述

代码

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);
			}
		}
	}
}

性能表现
性能表现

发布了75 篇原创文章 · 获赞 0 · 访问量 1518

猜你喜欢

转载自blog.csdn.net/qq_34087914/article/details/104087526