すべてのパス257再帰的なバイナリツリーの出力

バイナリツリーパス

タイトル説明
ここに画像を挿入説明

コード

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