# 38. 二叉树的所有路径

java

```java
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int x) {
        val = x;
    }

}


public class TreeUtils {

    public static int k=0;

    public static int k2=0;

    public static TreeNode stringToTree(String s){
        k=0;
        TreeNode root=null;
        root=createTree(root,s);
        return root;
    }

    private static TreeNode createTree(TreeNode node,String s){
        char ch=s.charAt(k++);
        if(ch=='#') return null;
        else{
            node=new TreeNode(ch-'0');
            node.left=createTree(node.left,s);
            node.right=createTree(node.right,s);
            return node;
        }
    }

    public static TreeNode stringToTreeWith(String str,String separator){
        k2=0;
        TreeNode root=null;
        String[] arr=str.trim().split(separator);
        root=createTreeWith(root,arr);
        return root;
    }

    private static TreeNode createTreeWith(TreeNode node,String[] arr){
        String v=arr[k2++];
        if(v.equals("#")) return null;
        else{
            node=new TreeNode(Integer.valueOf(v));
            node.left=createTreeWith(node.left,arr);
            node.right=createTreeWith(node.right,arr);
            return node;
        }
    }

    /**
     * 先序遍历
     * @param root
     */
    public static void travser(TreeNode root){
        if(root!=null){
            System.out.print(root.val+" ");
            travser(root.left);
            travser(root.right);
        }
    }

    /**
     * 中序遍历
     * @param root
     */
    public static void travser2(TreeNode root){
        if(root!=null){
            travser2(root.left);
            System.out.print(root.val+" ");
            travser2(root.right);
        }
    }

    /**
     * 后序遍历
     * @param root
     */
    public static void travser3(TreeNode root){
        if(root!=null){
            travser3(root.left);
            travser3(root.right);
            System.out.print(root.val+" ");
        }
    }
}



/**
 * 257. 二叉树的所有路径
 *
 * 给定一个二叉树，返回所有从根节点到叶子节点的路径。
 *
 * 说明: 叶子节点是指没有子节点的节点。
 *
 * 示例:
 *
 * 输入:
 *
 *    1
 *  /   \
 * 2     3
 *  \
 *   5
 *
 * 输出: ["1->2->5", "1->3"]
 *
 * 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3s
 */

import java.util.ArrayList;
import java.util.List;
public class Solution257 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list=new ArrayList<>();
        todo(root,list,"");
        return list;
    }

    public void todo(TreeNode root,List<String> list,String s) {
        String tmp=(s.length()==0?"":s+"->");
        if(root!=null){
            if(root.left==null&&root.right==null) {
                list.add(tmp+root.val);
            }else{
                todo(root.left,list,tmp+root.val);
                todo(root.right,list,tmp+root.val);
            }
        }
    }

    public static void main(String[] args){
        TreeNode root=TreeUtils.stringToTree("12#5##3##");
        List<String> r=new Solution257().binaryTreePaths(root);
        for(String s:r){
            System.out.println(s);
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/38-er-cha-shu-suo-you-lu-jin.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
